showBottomSheet<T> function
- required BuildContext context,
- required WidgetBuilder builder,
- Color? backgroundColor,
- double? elevation,
- ShapeBorder? shape,
- Color? barrierColor,
- bool isScrollControlled = true,
- bool isDismissible = true,
- bool enableDrag = true,
- RouteSettings? routeSettings,
- AnimationController? transitionAnimationController,
Shows a bottom sheet.
A bottom sheet is an alternative to a menu or a dialog and prevents the user from interacting with the rest of the app.
The context
argument is used to look up the Navigator and Theme for
the bottom sheet. It is only used when the method is called. Its
corresponding widget can be safely removed from the tree before the bottom
sheet is closed.
The isScrollControlled
parameter specifies whether this is a route for
a bottom sheet that will utilize DraggableScrollableSheet. If you wish
to have a bottom sheet that has a scrollable child such as a ListView or
a GridView and have the bottom sheet be draggable, you should set this
parameter to true.
The useRootNavigator
parameter ensures that the root navigator is used to
display the BottomSheet when set to true
. This is useful in the case
that a modal BottomSheet needs to be displayed above all other content
but the caller is inside another Navigator.
The isDismissible
parameter specifies whether the bottom sheet will be
dismissed when user taps on the scrim.
The enableDrag
parameter specifies whether the bottom sheet can be
dragged up and down and dismissed by swiping downwards.
The optional backgroundColor
, elevation
, shape
, clipBehavior
and
transitionAnimationController
parameters can be passed in to customize the
appearance and behavior of modal bottom sheets.
The transitionAnimationController
controls the bottom sheet's entrance and
exit animations if provided.
The optional routeSettings
parameter sets the RouteSettings of the modal bottom sheet
sheet. This is particularly useful in the case that a user wants to observe
PopupRoutes within a NavigatorObserver.
Returns a Future
that resolves to the value (if any) that was passed to
Navigator.pop when the modal bottom sheet was closed.
{@tool dartpad --template=stateless_widget_scaffold}
This example demonstrates how to use showBottomSheet
to display a
bottom sheet that obscures the content behind it when a user taps a button.
It also demonstrates how to close the bottom sheet using the Navigator
when a user taps on a button inside the bottom sheet.
Widget build(BuildContext context) {
return Center(
child: ElevatedButton(
child: const Text('showBottomSheet'),
onPressed: () {
showBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return Container(
height: 200,
color: Colors.amber,
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const Text('Modal BottomSheet'),
ElevatedButton(
child: const Text('Close BottomSheet'),
onPressed: () => Navigator.pop(context),
)
],
),
),
);
},
);
},
),
);
}
{@end-tool} See also:
- BottomSheet, a helper widget that implements the fluent ui bottom and top sheet
- DraggableScrollableSheet, which allows you to create a bottom sheet that grows and then becomes scrollable once it reaches its maximum size.
Implementation
Future<T?> showBottomSheet<T>({
required BuildContext context,
required WidgetBuilder builder,
Color? backgroundColor,
double? elevation,
ShapeBorder? shape,
Color? barrierColor,
bool isScrollControlled = true,
bool useRootNavigator = false,
bool isDismissible = true,
bool enableDrag = true,
RouteSettings? routeSettings,
AnimationController? transitionAnimationController,
}) {
assert(debugCheckHasMediaQuery(context));
assert(debugCheckHasFluentLocalizations(context));
final NavigatorState navigator =
Navigator.of(context, rootNavigator: useRootNavigator);
return navigator.push(_ModalBottomSheetRoute<T>(
builder: builder,
capturedThemes:
InheritedTheme.capture(from: context, to: navigator.context),
isScrollControlled: isScrollControlled,
barrierLabel: FluentLocalizations.of(context).modalBarrierDismissLabel,
backgroundColor: backgroundColor,
elevation: elevation,
shape: shape,
isDismissible: isDismissible,
modalBarrierColor: barrierColor,
enableDrag: enableDrag,
settings: routeSettings,
transitionAnimationController: transitionAnimationController,
));
}