leading static method
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,
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 */,
);
}