myAppBar function

PreferredSizeWidget myAppBar({
  1. bool canPop = true,
  2. Color? backgroundColor,
  3. required String title,
  4. List<Widget>? actions,
  5. Widget? leading,
  6. bool? centerTitle,
  7. double? leadingWidth,
  8. required bool defaultFlexibleSpace,
  9. double? toolbarHeight,
  10. PreferredSizeWidget? bottom,
  11. Function? back,
})

自定义AppBar

  • canPop 是否显示返回按钮
  • backgroundColor背景色
  • titleappBar标题
  • leadingwidget类型,如果为空默认返回ios风格返回按钮
  • actions右侧图标,List

Implementation

PreferredSizeWidget myAppBar(
    {bool canPop = true,
    Color? backgroundColor,
    required String title,
    List<Widget>? actions,
    Widget? leading,
    bool? centerTitle,
    double? leadingWidth,
    required bool defaultFlexibleSpace,
    double? toolbarHeight,
    PreferredSizeWidget? bottom,
    Function? back}) {
  return AppBar(
    backgroundColor: backgroundColor ?? Colors.white,
    toolbarHeight: toolbarHeight ?? 44.w,
    elevation: 0,
    centerTitle: centerTitle ?? true,
    title: SizedBox(
      height: 44.w,
      child: Column(
        children: [
          const Spacer(),
          Container(
            // color: Colors.blue,
            margin: EdgeInsets.only(bottom: 10.w),
            child: Text(
              title,
              style: TextStyle(
                fontSize: 18.sp,
                color: defaultFlexibleSpace ? Colors.white : AppColor.mainText,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
        ],
      ),
    ),
    leadingWidth: 50.h,
    leading: canPop
        ? IconButton(
          iconSize: 18.w,
          // splashRadius: 25.w,
          // padding: EdgeInsets.zero,
          alignment: Alignment.center,
          onPressed: () {
            if (back != null) {
              back.call();
            } else {
              Get.back();
            }
          },
          icon: Icon(
            Icons.arrow_back_ios_new,
            color: defaultFlexibleSpace ? Colors.white : AppColor.mainText,
            size: 18.w,
          ),
        )
        : null,
    actions: actions,
    // flexibleSpace: defaultFlexibleSpace
    //     ? Container(
    //         decoration: BoxDecoration(
    //           gradient: LinearGradient(
    //             colors: [
    //               AppColor.radientStart,
    //               AppColor.radientEnd,
    //             ],
    //             begin: Alignment.topCenter,
    //             end: Alignment.bottomCenter,
    //           ),
    //         ),
    //       )
    //     : null,
    automaticallyImplyLeading: false,
    bottom: bottom,
  );
}