import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/utils/font_weight.dart';

class MyAppBar extends StatelessWidget implements PreferredSizeWidget {
  final Function onTap;
  final Widget action;
  List<Widget> actions;
  final Widget bottom;
  final Widget titleChild;
  final String title;
  final double titleSize;
  final Color titleColor;
  final Color leadingColor;
  final Color background;
  final Size preferredSize;
  final double toolbarHeight;
  final bool leading;
  final bool centerTitle;
  final Brightness brightness;
  final Function exit;

  MyAppBar({
    Key key,
    this.onTap,
    this.action,
    this.actions,
    this.bottom,
    this.titleChild,
    this.title,
    this.toolbarHeight,
    this.background = const Color(0xFFF7F7F7),
    this.leadingColor = Colors.black,
    this.titleColor = Colors.black,
    this.titleSize = 18,
    this.brightness = Brightness.light,
    this.leading = true,
    this.centerTitle = false,
    this.exit
  })  : preferredSize = Size.fromHeight(toolbarHeight ?? kToolbarHeight),
        super(key: key) {
    if (actions == null)
      actions = [];
    if (action != null) {
      actions.add(
        Container(
          margin: EdgeInsets.only(right: 15),
          child: GestureDetector(
            onTap: onTap,
            child: action,
          ),
        ),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return AppBar(
      backgroundColor: background,
      elevation: 0,
      brightness: brightness,
      leading: leading
          ? GestureDetector(
          behavior: HitTestBehavior.opaque,
              onTap: () {
                if(exit == null)
                Navigator.of(context).pop();
                else exit();
              },
              child: Container(
                alignment: Alignment.centerRight,
                margin: EdgeInsets.only(left: 12,),
                padding: EdgeInsets.all(6),
                child: Icon(
                  Icons.arrow_back_ios,
                  color: leadingColor,
                  size: 24,
                ),
              ),
            )
          : Container(),
      titleSpacing: 2,
      centerTitle: centerTitle,
      leadingWidth: leading ? 56 : 10.w,
      title: ((title == null || title == "") && titleChild != null)
          ? titleChild
          : GestureDetector(
        onTap: () {
          if(exit == null)
            Navigator.of(context).pop();
          else exit();
        },
        child: Container(
          width: double.infinity,
          margin: EdgeInsets.only(right:34.w),
          padding: EdgeInsets.all(10),
          alignment: Alignment.center,
          child: Text(
            title,
            style: TextStyle(
              color: titleColor,
              fontWeight: MyFontWeight.medium,
              fontSize: titleSize,
            ),
          ),
        ),
      ),
      actions: actions,
      bottom: bottom,
    );
  }
}