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

import 'package:yumi/app/constants/sc_global_config.dart';
import 'package:yumi/app/constants/sc_screen.dart';

class SocialChatAppBar extends StatefulWidget implements PreferredSizeWidget {
  final Widget child;
  final double height;
  final backgroundColor;

  //LinearGradient gradient;

  @override
  State<StatefulWidget> createState() {
    return SocialChatAppBarState();
  }

  SocialChatAppBar({
    required this.child,
    this.height = kToolbarHeight,
    this.backgroundColor = Colors.white,
    //this.gradient = const LinearGradient(colors: [Colors.white, Colors.white] , begin: Alignment.topLeft, end: Alignment.bottomRight)
  });

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

class SocialChatAppBarState extends State<SocialChatAppBar> {
  @override
  Widget build(BuildContext context) {
    //    SystemUiOverlayStyle style = SystemUiOverlayStyle(
    //        statusBarColor: Colors.white,
    //        ///这是设置状态栏的图标和字体的颜色
    //        ///Brightness.light  一般都是显示为白色
    //        ///Brightness.dark 一般都是显示为黑色
    //        statusBarIconBrightness: Brightness.dark
    //    );
    //    SystemChrome.setSystemUIOverlayStyle(style);
    return Container(
      color: widget.backgroundColor,
      //decoration: BoxDecoration(gradient: widget.gradient),
      child: SafeArea(
        top: true,
        child: Container(height: widget.height, child: widget.child),
      ),
    );
  }
}

class SocialChatStandardAppBar extends StatelessWidget
    implements PreferredSizeWidget {
  final String title;
  final Widget? titleWidget;
  final Widget? leading;
  final List<Widget> actions;
  final Color backgroundColor;
  final Color? backButtonColor;
  final Gradient gradient;
  final Function? onTag;
  final bool showBackButton;

  SocialChatStandardAppBar({
    this.title = "",
    required this.actions,
    this.backgroundColor = Colors.white,
    this.backButtonColor,
    this.titleWidget,
    this.showBackButton = true,
    this.leading,
    this.onTag,
    this.gradient = const LinearGradient(
      colors: [Colors.transparent, Colors.transparent],
    ),
  });

  @override
  Widget build(BuildContext context) {
    var leadingWidget = Container(
      width: titleWidget == null ? 38 : null,
      alignment: AlignmentDirectional.centerStart,
      child:
          !showBackButton
              ? Container()
              : leading ??
                  GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () {
                      if (onTag != null) {
                        onTag!();
                      } else {
                        Navigator.pop(context);
                      }
                    },
                    child: Container(
                      width: width(50),
                      height: height(30),
                      alignment: AlignmentDirectional.centerStart,
                      padding: EdgeInsetsDirectional.only(start: width(15)),
                      child: Icon(
                        SCGlobalConfig.lang == "ar"
                            ? Icons.keyboard_arrow_right
                            : Icons.keyboard_arrow_left,
                        size: 28.w,
                        color: backButtonColor ?? Color(0xffffffff),
                      ),
                    ),
                  ),
    );
    var titleBody =
        titleWidget ??
        Align(
          alignment: Alignment.center,
          child: Text(
            title,
            textAlign: TextAlign.center,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(18),
              color: backButtonColor ?? Color(0xffffffff),
              fontWeight: FontWeight.w500,
            ),
          ),
        );
    var body;
    if (titleWidget != null) {
      body = Container(
        margin: EdgeInsets.only(top: ScreenUtil().statusBarHeight),
        child: Row(
          children: <Widget>[
            leadingWidget,
            Expanded(child: titleBody),
            if (actions != null)
              Row(mainAxisAlignment: MainAxisAlignment.end, children: actions),
          ],
        ),
      );
    }
    return Container(
      decoration: BoxDecoration(
        gradient:
            gradient ??
            LinearGradient(colors: [Color(0xffffffff), Color(0xffffffff)]),
      ),
      child:
          body ??
          Container(
            margin: EdgeInsets.only(top: ScreenUtil().statusBarHeight),
            child: Stack(
              alignment: AlignmentDirectional.centerStart,
              children: <Widget>[
                leadingWidget,
                titleBody,
                if (actions != null)
                  Align(
                    alignment: AlignmentDirectional.centerEnd,
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      mainAxisAlignment: MainAxisAlignment.end,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: actions,
                    ),
                  ),
              ],
            ),
          ),
    );
  }

  @override
  // TODO: implement preferredSize
  Size get preferredSize => Size.fromHeight(kToolbarHeight);
}
