import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:liftmanager/res/colors.dart';
import 'package:liftmanager/res/dimens.dart';
import 'package:liftmanager/res/gaps.dart';
import 'package:liftmanager/res/iconfont.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';

/// 自定义AppBar
class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  const MyAppBar(
      {Key key,
      this.backgroundColor,
      this.title: "",
      this.centerTitle: "",
      this.actions,
      this.onPressed,
      this.fun,
      this.onBack,
      this.isFun: false,
      this.isBack: true,
      this.bgColors,
      this.titleColor = Colours.text_black,
      this.statubarStyle = SystemUiOverlayStyle.dark,
      this.isColorTopToBottom=false})
      : super(key: key);
// SystemUiOverlayStyle.dark
  final Color backgroundColor;
  final String title;
  final String centerTitle;
  final List<Widget> actions;
  final VoidCallback onPressed;
  final bool isBack;
  final Function fun;
  final bool isFun;
  final VoidCallback onBack;
  final List<Color> bgColors;
  final Color titleColor;
  final SystemUiOverlayStyle statubarStyle;
  final bool isColorTopToBottom;
  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: statubarStyle,
      child: Container(
        color: bgColors == null
            ? backgroundColor == null
                ? Colors.white
                : backgroundColor
            : null,
        decoration: bgColors != null
            ? BoxDecoration(
                gradient: LinearGradient(
                  begin: isColorTopToBottom?Alignment.topCenter:Alignment.centerLeft,
                  end: isColorTopToBottom?Alignment.bottomCenter:Alignment.centerRight,
                  colors: bgColors,
                ),
              )
            : null,
        child: SafeArea(
          child: Stack(
            alignment: Alignment.centerLeft,
            children: <Widget>[
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Container(
                    alignment: centerTitle.isEmpty
                        ? Alignment.centerLeft
                        : Alignment.center,
                    width: double.infinity,
                    child: Text(title.isEmpty ? centerTitle : title,
                        style: TextStyle(
                          fontSize: Dimens.font_sp18,
                          color: titleColor,
                        )),
                    padding: const EdgeInsets.symmetric(horizontal: 48.0),
                  )
                ],
              ),
              isBack
                  ? IconButton(
                      onPressed: () {
                        // FocusScope.of(context).unfocus();
                        // if(isFun){
                        //   fun();
                        // }else {
                        //   Navigator.maybePop(context);
                        // }

                        if (onBack != null) {
                          onBack();
                        } else if (isFun) {
                          fun();
                          FocusScope.of(context).unfocus();
                        } else {
                          FocusScope.of(context).unfocus();
                          NavigatorUtils.goBackWithParams(context, true);
                        }
                      },
                      tooltip: 'Back',
                      padding: const EdgeInsets.all(12.0),
                      icon: Icon(
                        Iconfont.fanhui,
                        size: 17,
                        color: titleColor,
                      ),
                    )
                  : Gaps.empty,
              Positioned(
                right: 10,
                child: Theme(
                    data: Theme.of(context).copyWith(
                        buttonTheme: ButtonThemeData(
                      padding: const EdgeInsets.symmetric(horizontal: 16.0),
                      minWidth: 60.0,
                    )),
                    child: rightActions()),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget rightActions() {
    if (this.actions != null && this.actions.isNotEmpty) {
      return Row(
        children: this.actions,
      );
    }

    return Container(child: null,);
  }

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