mAppBar function

PreferredSizeWidget mAppBar({
  1. String? route,
  2. String title = '',
  3. bool? centerTitle,
  4. Color? titleColor,
  5. double? titleFontSize,
  6. FontWeight? titleFontWeight,
  7. int titleMaxLine = 1,
  8. double height = 0,
  9. double? elevation,
  10. bool backEnable = false,
  11. bool autoBackEnable = true,
  12. Color? backgroundColor,
  13. IconData? backIconData,
  14. Color? backIconColor,
  15. Widget? leading,
  16. VoidCallback? backPressed,
  17. List<Widget>? actions,
  18. PreferredSizeWidget? bottom,
  19. double? titleSpacing,
  20. double? leadingWidth,
  21. double? backIconSize = 20,
  22. Widget? titleView,
  23. SystemUiOverlayStyle? systemOverlayStyle,
  24. bool automaticallyImplyLeading = true,
  25. String? semanticsLabel,
  26. double? scrolledUnderElevation,
  27. dynamic backResult,
  28. EdgeInsetsGeometry? padding,
})

统一appbar

Implementation

PreferredSizeWidget mAppBar({
  String? route,
  String title = '',
  bool? centerTitle,
  Color? titleColor,
  double? titleFontSize,
  FontWeight? titleFontWeight,
  int titleMaxLine = 1,
  double height = 0,
  double? elevation,
  bool backEnable = false,
  bool autoBackEnable = true,
  Color? backgroundColor,
  IconData? backIconData,
  Color? backIconColor, // 左侧返回按钮
  Widget? leading, // 自定义左侧按钮
  VoidCallback? backPressed,
  List<Widget>? actions,
  PreferredSizeWidget? bottom,
  double? titleSpacing,
  double? leadingWidth,
  double? backIconSize = 20,
  Widget? titleView,
  SystemUiOverlayStyle? systemOverlayStyle,
  bool automaticallyImplyLeading = true,
  String? semanticsLabel,
  double? scrolledUnderElevation,
  dynamic backResult,
  EdgeInsetsGeometry? padding,
}) {
  if (autoBackEnable) {
    backEnable = Nav.isPopEnable(route: route);
  }

  /// 默认返回按钮
  final Widget leadingDefault = IconButton(
    icon: Icon(
      backIconData ?? Icons.arrow_back_ios_new,
      color: backIconColor,
      size: backIconSize,
    ),
    onPressed: backPressed ??
        () {
          clickBack(result: backResult, route: route);
        },
  );
  Widget? leadingReal;
  if (leading != null) {
    leadingReal = leading;
  } else if (backEnable || backPressed != null) {
    leadingReal = leadingDefault;
  } else {
    leadingReal = const SizedBox.shrink();
    leadingWidth = 0;
  }
  var bar = AppBar(
    leading: leadingReal,
    elevation: elevation,
    scrolledUnderElevation: scrolledUnderElevation,
    backgroundColor: backgroundColor,
    centerTitle: centerTitle,
    automaticallyImplyLeading: automaticallyImplyLeading,
    title: titleView ??
        Builder(
          builder: (ctx) => mText(
            title,
            maxLines: titleMaxLine,
            semanticsLabel: semanticsLabel,
            style: Theme.of(ctx).appBarTheme.titleTextStyle?.copyWith(
                  fontWeight: titleFontWeight,
                  color: titleColor,
                  fontSize: titleFontSize,
                ),
          ),
        ),
    actions: actions,
    bottom: bottom,
    titleSpacing: titleSpacing,
    leadingWidth: leadingWidth,
    systemOverlayStyle: systemOverlayStyle ?? getSystemOverlayStyle(),
  );

  double pHeight = (height != 0 ? height : BaseDimens.dAppBarHeight) + (bottom?.preferredSize.height ?? 0);

  return PreferredSize(
      preferredSize: Size.fromHeight(pHeight),
      child: padding == null
          ? bar
          : Container(
              padding: padding,
              child: bar,
            ));
}