showSlidingBottomSheet<T> function

Future<T?> showSlidingBottomSheet<T>(
  1. BuildContext context, {
  2. required SlidingSheet 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 SlidingSheet Function(BuildContext context) builder,
  Widget Function(BuildContext context, SlidingSheet sheet)? parentBuilder,
  RouteSettings? routeSettings,
  bool useRootNavigator = false,
  bool resizeToAvoidBottomInset = true,
}) {
  SlidingSheet dialog = builder(context);

  final SheetController controller = dialog.controller ?? SheetController();
  final ValueNotifier<int> rebuilder = ValueNotifier(0);

  return Navigator.of(
    context,
    rootNavigator: useRootNavigator,
  ).push(
    _SheetRoute(
      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++;
            };

            Widget sheet = dialog;

            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;
          },
        );
      },
    ),
  );
}