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/utils/theme_utils.dart';
import 'package:liftmanager/widgets/load_image.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})
      : super(key: key);

  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;

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light,
      child: 
      Container(
        decoration: BoxDecoration(
          gradient: const LinearGradient(
              colors: [Color(0xFF00D9FF), Color(0xFF0287FF)]),
        ),
        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: Colours.dark_text,
                        )),
                    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(
                        Icons.arrow_back_ios,
                        color: Colours.dark_text,
                      ),
                    )
                  : Gaps.empty,
              Positioned(
                right: 0.0,
                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,
      );
    }else{
      return Container();
    }
  }

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