my_app_bar.dart 4.38 KB
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:one_poem/util/theme_utils.dart';
import 'package:one_poem/res/resources.dart';

/// 自定义AppBar
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  const MyAppBar({
    Key? key,
    this.backgroundColor,
    this.title = '',
    this.centerTitle = '',
    this.actionName = '',
    this.backImg = 'assets/images/ic_back_black.png',
    this.backImgColor,
    this.onPressed,
    this.isBack = true,
    this.buttonLeft,
    this.funcLeft,
    this.buttonCenter,
    this.funcCenter,
    this.buttonRight,
    this.funcRight,
    this.isShowButtons = false,
  }) : super(key: key);

  final Color? backgroundColor;
  final String title;
  final String centerTitle;
  final String backImg;
  final Color? backImgColor;
  final String actionName;
  final VoidCallback? onPressed;
  final bool isBack;
  final bool isShowButtons;

  final String? buttonLeft;
  final Function? funcLeft;
  final String? buttonCenter;
  final Function? funcCenter;
  final String? buttonRight;
  final Function? funcRight;

  @override
  Widget build(BuildContext context) {
    final Color _backgroundColor = backgroundColor ?? context.backgroundColor;

    final SystemUiOverlayStyle _overlayStyle =
        ThemeData.estimateBrightnessForColor(_backgroundColor) ==
                Brightness.dark
            ? SystemUiOverlayStyle.light
            : SystemUiOverlayStyle.dark;

    final Widget action = actionName.isNotEmpty
        ? Positioned(
            right: 0.0,
            child: Theme(
              data: Theme.of(context).copyWith(
                buttonTheme: const ButtonThemeData(
                  padding: EdgeInsets.symmetric(horizontal: 16.0),
                  minWidth: 60.0,
                ),
              ),
              child: Row(
                children: [
                  Icon(
                    Icons.star,
                    color: Colors.white.withOpacity(0.66),
                  ),
                  Gaps.hGap10,
                  Icon(
                    Icons.ios_share,
                    color: Colors.white.withOpacity(0.66),
                  ),
                  Gaps.hGap10,
                ],
              ),
            ),
          )
        : Gaps.empty;

    final Widget back = isBack
        ? IconButton(
            onPressed: () async {
              FocusManager.instance.primaryFocus?.unfocus();
              final isBack = await Navigator.maybePop(context);
              if (!isBack) {
                await SystemNavigator.pop();
              }
            },
            tooltip: '返回',
            padding: const EdgeInsets.all(12.0),
            icon: Image.asset(
              backImg,
              color: backImgColor ?? ThemeUtils.getIconColor(context),
            ),
          )
        : Gaps.empty;

    // TODO 复用组件
    final Widget titleWidget = Semantics(
      namesRoute: true,
      header: true,
      child: Container(
        alignment: Alignment.center,
        width: double.infinity,
        margin: const EdgeInsets.symmetric(horizontal: 48.0),
        child: isShowButtons ? Row(
          children: [
            TextButton(
              onPressed: () => funcLeft!(),
              child: Text(
                buttonLeft ?? "一言",
                style: const TextStyle(color: Colors.white),
              ),
            ),
            const Text("|"),
            TextButton(
              onPressed: () => funcCenter!(),
              child: Text(
                buttonCenter ?? "译解",
                style: const TextStyle(color: Colors.white),
              ),
            ),
            const Text("|"),
            TextButton(
              onPressed: () => funcRight!(),
              child: Text(
                buttonRight ?? "临境",
                style: const TextStyle(color: Colors.white),
              ),
            ),
          ],
        ) : Gaps.hGap10,
      ),
    );

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
        color: Colors.transparent,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              titleWidget,
              back,
              action,
            ],
          ),
        ),
      ),
    );
  }

  @override
  Size get preferredSize => const Size.fromHeight(48.0);
}