
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 自定义AppBar
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {

  const MyAppBar({
    Key? key,
    this.backgroundColor,
    this.title = '',
    this.centerTitle = '',
    this.actionName = '',
    this.backImg = 'assets/images/navigator/back.png',
    this.backImgColor,
    this.onPressed,
    this.isDiyBack = false,
    this.superContext,
    this.isBack = true
  }): 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 isDiyBack;
  final BuildContext? superContext;

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

    final SystemUiOverlayStyle overlayStyle = ThemeData.estimateBrightnessForColor(bgColor) == Brightness.dark
        ? SystemUiOverlayStyle.light : SystemUiOverlayStyle.dark;

    final Widget action =  Container();

    final Widget back = isBack ? IconButton(
      onPressed: () async {
        if (isDiyBack) {
          Navigator.pop(superContext!);
        } else {
          FocusManager.instance.primaryFocus?.unfocus();
          final isBack = await Navigator.maybePop(context);
          if (!isBack) {
            await SystemNavigator.pop();
          }
        }
      },
      padding: EdgeInsets.only(left: 15.sp),
      icon: Image.asset(
        backImg,
        width: 24.sp,
        height: 24.sp,
      ),
    ) : Container();

    final Widget titleWidget = Semantics(
      namesRoute: true,
      header: true,
      child: Container(
        alignment: centerTitle.isEmpty ? Alignment.centerLeft : Alignment.center,
        width: double.infinity,
        margin: const EdgeInsets.symmetric(horizontal: 48.0),
        child: ShaderMask(
          shaderCallback: (Rect bounds) {
            return const LinearGradient(
              begin: Alignment(0.0,-1.0),
              end: Alignment.bottomCenter,
              colors: [Color(0xff71F3F2), Color(0xffF657FF)],
            ).createShader(Offset.zero & bounds.size);
          },
          child:Text(
          title.isEmpty ? centerTitle : title,
          style: TextStyle(fontSize: 18.sp,fontWeight: FontWeight.w600,color: Colors.white,shadows: [Shadow(color: Color(0xffF657FF), offset: Offset(0.0, -1))]),
        ),
      ),
    ));
    
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: overlayStyle,
      child: Material(
        color: bgColor,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              titleWidget,
              back,
              action,
            ],
          ),
        ),
      ),
    );
  }

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