leading static method

Widget? leading({
  1. required BuildContext context,
  2. bool forceUseBuilder = false,
  3. bool allowDrawerButton = true,
  4. bool allowPopButton = true,
  5. String? forceTooltip,
  6. VoidCallback? forceOnLongPress,
  7. double? iconSize,
  8. VisualDensity? visualDensity,
  9. EdgeInsetsGeometry? padding,
  10. AlignmentGeometry? alignment,
  11. double? splashRadius,
  12. Color? focusColor,
  13. Color? hoverColor,
  14. Color? color,
  15. Color? splashColor,
  16. Color? highlightColor,
  17. Color? disabledColor,
  18. MouseCursor? mouseCursor,
  19. FocusNode? focusNode,
  20. bool? autofocus,
  21. bool? enableFeedback,
  22. BoxConstraints? constraints,
})

Creates the default AppBarActionButton which will be used as leading button in AppBar.

Note that if you want to use leading as Scaffold.appBar's leading directly (which means do not wrap any widgets right under Scaffold) and want to display the correct drawer button, please set forceUseBuilder to true.

forceUseBuilder ensures ScaffoldState is available by using Builder to get the correct context, but it will also make leading never return null. So if there is no drawer button, close button or back button, a blank with AppBar.leadingWidth width will be displayed.

Implementation

static Widget? leading({
  required BuildContext context,
  bool forceUseBuilder = false,
  bool allowDrawerButton = true,
  bool allowPopButton = true,
  String? forceTooltip,
  VoidCallback? forceOnLongPress,
  // <<<
  double? iconSize,
  VisualDensity? visualDensity,
  EdgeInsetsGeometry? padding,
  AlignmentGeometry? alignment,
  double? splashRadius,
  Color? focusColor,
  Color? hoverColor,
  Color? color,
  Color? splashColor,
  Color? highlightColor,
  Color? disabledColor,
  MouseCursor? mouseCursor,
  FocusNode? focusNode,
  bool? autofocus,
  bool? enableFeedback,
  BoxConstraints? constraints,
}) {
  Widget? _build(BuildContext context) {
    final scaffold = Scaffold.maybeOf(context);
    final hasDrawer = scaffold?.hasDrawer ?? false; // no consideration of hasEndDrawer

    final parentRoute = ModalRoute.of(context);
    final canPop = parentRoute?.canPop ?? false;
    final useCloseButton = parentRoute is PageRoute<dynamic> && parentRoute.fullscreenDialog;

    Widget icon;
    String tooltip;
    VoidCallback onPressed;
    if (allowDrawerButton && hasDrawer) {
      icon = const Icon(Icons.menu);
      tooltip = MaterialLocalizations.of(context).openAppDrawerTooltip;
      onPressed = () => Scaffold.of(context).openDrawer();
    } else if (allowPopButton && canPop) {
      if (useCloseButton) {
        icon = const Icon(Icons.close);
        tooltip = MaterialLocalizations.of(context).closeButtonTooltip;
        onPressed = () => Navigator.maybePop(context);
      } else {
        icon = const BackButtonIcon(); // Icons.arrow_back / Icons.arrow_back_ios
        tooltip = MaterialLocalizations.of(context).backButtonTooltip;
        onPressed = () => Navigator.maybePop(context);
      }
    } else {
      return null;
    }

    return AppBarActionButton(
      icon: icon,
      onPressed: onPressed,
      tooltip: forceTooltip ?? tooltip,
      onLongPress: forceOnLongPress,
      // ===
      iconSize: iconSize,
      visualDensity: visualDensity,
      padding: padding,
      alignment: alignment,
      splashRadius: splashRadius,
      focusColor: focusColor,
      hoverColor: hoverColor,
      color: color,
      splashColor: splashColor,
      highlightColor: highlightColor,
      disabledColor: disabledColor,
      mouseCursor: mouseCursor,
      focusNode: focusNode,
      autofocus: autofocus,
      enableFeedback: enableFeedback,
      constraints: constraints,
    );
  }

  if (!forceUseBuilder) {
    return _build(context); // nullable
  }
  return Builder(
    builder: (c) =>
        _build(c) ?? //
        const SizedBox.shrink() /* display a blank with fixed width */,
  );
}