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

/// 自定义AppBar
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  const MyAppBar({
    Key? key,
    this.backgroundColor,
    this.onPressed,
    this.isBack = true,
    this.homeMenuHeader,
    this.homeActionWidgets,
    this.isTransparent = false,
  }) : super(key: key);

  final Color? backgroundColor;
  final VoidCallback? onPressed;
  final bool isBack;
  final bool isTransparent;

  final Widget? homeMenuHeader;
  final Widget? homeActionWidgets;

  @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 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: Icon(
              Icons.arrow_back_ios_outlined,
              color: isTransparent ? Colors.black54 : Colors.white,
            ),
          )
        : Gaps.empty;

    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: _overlayStyle,
      child: Material(
        color: isTransparent ? Colors.transparent : Colors.black,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.center,
            children: <Widget>[
              Positioned(
                left: 5,
                child: back,
              ),
              SizedBox(
                width: MediaQuery.of(context).size.width,
                child: homeMenuHeader,
              ),
              SizedBox(
                child: Container(
                  padding: const EdgeInsets.only(top: 10.0),
                  alignment: Alignment.centerRight,
                  child: homeActionWidgets,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

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