showSlidingBottomSheet<T> function

Future<T?> showSlidingBottomSheet<T>(
  1. BuildContext context, {
  2. required SlidingSheetDialog builder(
    1. BuildContext context
    ),
  3. Widget parentBuilder(
    1. BuildContext context,
    2. SlidingSheet sheet
    )?,
  4. RouteSettings? routeSettings,
  5. bool useRootNavigator = false,
  6. bool resizeToAvoidBottomInset = true,
})

Shows a SlidingSheet as a material design bottom sheet.

The builder parameter must not be null and is used to construct a SlidingSheetDialog.

The parentBuilder parameter can be used to wrap the sheet inside a parent, for example a Theme or AnnotatedRegion.

The routeSettings argument, see RouteSettings for details.

The resizeToAvoidBottomInset parameter can be used to avoid the keyboard from obscuring the content bottom sheet.

Implementation

Future<T?> showSlidingBottomSheet<T>(
  BuildContext context, {
  required SlidingSheetDialog Function(BuildContext context) builder,
  Widget Function(BuildContext context, SlidingSheet sheet)? parentBuilder,
  RouteSettings? routeSettings,
  bool useRootNavigator = false,
  bool resizeToAvoidBottomInset = true,
}) {
  SlidingSheetDialog dialog = builder(context);
  final SheetController controller = dialog.controller ?? SheetController();

  final theme = Theme.of(context);
  final ValueNotifier<int> rebuilder = ValueNotifier(0);

  return Navigator.of(
    context,
    rootNavigator: useRootNavigator,
  ).push(
    _SlidingSheetRoute(
      duration: dialog.duration,
      settings: routeSettings,
      builder: (context, animation, route) {
        return ValueListenableBuilder(
          valueListenable: rebuilder,
          builder: (context, dynamic value, _) {
            dialog = builder(context);

            // Assign the rebuild function in order to
            // be able to change the dialogs parameters
            // inside a dialog.
            controller._rebuild = () {
              rebuilder.value++;
            };

            var snapSpec = dialog.snapSpec;
            if (snapSpec.snappings.first != 0.0) {
              snapSpec = snapSpec.copyWith(
                snappings: [0.0] + snapSpec.snappings,
              );
            }

            Widget sheet = SlidingSheet._(
              route: route,
              controller: controller,
              builder: dialog.builder,
              customBuilder: dialog.customBuilder,
              headerBuilder: dialog.headerBuilder,
              footerBuilder: dialog.footerBuilder,
              listener: dialog.listener,
              snapSpec: snapSpec,
              duration: dialog.duration,
              color: dialog.color ??
                  theme.bottomSheetTheme.backgroundColor ??
                  theme.dialogTheme.backgroundColor ??
                  theme.dialogBackgroundColor,
              backdropColor: dialog.backdropColor,
              shadowColor: dialog.shadowColor,
              elevation: dialog.elevation,
              padding: dialog.padding,
              avoidStatusBar: dialog.avoidStatusBar,
              margin: dialog.margin,
              border: dialog.border,
              cornerRadius: dialog.cornerRadius,
              cornerRadiusOnFullscreen: dialog.cornerRadiusOnFullscreen,
              closeOnBackdropTap: dialog.dismissOnBackdropTap,
              scrollSpec: dialog.scrollSpec,
              maxWidth: dialog.maxWidth,
              closeSheetOnBackButtonPressed: false,
              minHeight: dialog.minHeight,
              isDismissable: dialog.isDismissable,
              onDismissPrevented: dialog.onDismissPrevented,
              isBackdropInteractable: dialog.isBackdropInteractable,
              axisAlignment: dialog.axisAlignment,
              extendBody: dialog.extendBody,
              liftOnScrollHeaderElevation: dialog.liftOnScrollHeaderElevation,
              liftOnScrollFooterElevation: dialog.liftOnScrollFooterElevation,
              body: null,
            );

            if (parentBuilder != null) {
              sheet = parentBuilder(context, sheet as SlidingSheet);
            }

            if (resizeToAvoidBottomInset) {
              sheet = Padding(
                padding: EdgeInsets.only(
                  bottom: MediaQuery.of(context).viewInsets.bottom,
                ),
                child: sheet,
              );
            }

            return sheet;
          },
        );
      },
    ),
  );
}