modal_bottom_sheet

repository·main·Indexed 23 days ago

https://github.com/jamesblasco/modal_bottom_sheet

A Flutter package for creating highly customizable modal bottom sheets supporting Material and Cupertino styles. It features scroll-syncing via ModalScrollController, nested navigation, and improved route animations through MaterialWithModalsPageRoute. The package includes specialized options like Bar Modals and an experimental Sheet widget for draggable, embeddable bottom sheets with programmatic control via SheetController.

Tokens
7.5K
Snippets
14
Records
54
Agent score
83%

What's inside modal_bottom_sheet

  1. Overview of modal_bottom_sheet

    main
    The modal_bottom_sheet package provides a modal bottom sheet implementation for Flutter applications. It allows developers to present content in a sheet that slides up from the bottom of the screen, a common UI pattern in mobile applications.
  2. Why use modal_bottom_sheet instead of Flutter's showModalBottomSheet

    main

    The modal_bottom_sheet package is inspired by Flutter's built-in showModalBottomSheet but provides several critical features missing from the standard implementation:

    • Scrollview Integration: Supports dragging down to close when the sheet is inside a ScrollView (standard showModalBottomSheet often fails in these scenarios).
    • Pop Control: Supports WillPopScope to allow developers to prevent the dialog from closing unexpectedly.
    • iOS Enhancements: Includes support for 'scroll to top' when tapping the status bar on iOS.
    • Cupertino Support: Provides a native-feeling Cupertino modal bottom sheet.
    • Customization: Enables the creation of highly custom modal bottom sheets.
  3. Implement navigation inside a modal bottom sheet

    main

    You can implement navigation within a modal bottom sheet using the following patterns:

    1. Stacking Sheets: Call showCupertinoModalBottomSheet again to push a new modal bottom sheet on top of the current one.
    2. Internal Navigation: Add a new Navigator or CupertinoTabScaffold inside the builder of the bottom sheet to allow navigation within the sheet itself.
    3. Preventing Closure: Use Flutter's WillPopScope inside the sheet to intercept back gestures and prevent the modal from closing unexpectedly.
  4. Push new views inside a modal bottom sheet

    main

    You can navigate within a modal bottom sheet using these patterns:

    • Stacking Modals: To push a new modal on top of the current one, simply call showCupertinoModalBottomSheet (or the Material equivalent) again.
    • Internal Navigation: To implement standard navigation inside the sheet, add a new Navigator or CupertinoTabScaffold as the child of the modal's builder.
    • Intercepting Back Buttons: The package supports Flutter's WillPopScope (or PopScope) to prevent the modal from closing unexpectedly.
  5. Use the experimental Sheet package

    main
    The author is developing a new package called Sheet which reimplements the modal bottom sheet behavior from scratch. Sheet is intended to be easier to use, more performant, more stable, and more customizable than the current implementation. It is currently considered experimental but is already used in several released applications.
  6. How the Sheet widget works

    main

    The Sheet widget is a draggable bottom sheet that can be embedded directly into a page. To ensure it appears above your page content, you should typically wrap your page body and the Sheet widget in a Stack.

    Key capabilities include:

    • Clamping: Limit the sheet's movement between minExtent and maxExtent.
    • Interaction Area: Use minInteractionExtent to allow users to drag the sheet up even when it is fully hidden (e.g., when initialExtent is 0).
    • Fit Modes: Use SheetFit.expand to force the child to take up the maximum available height.
    • Resizing: Setting resizable: true allows the sheet to change the actual height of its child instead of just translating it vertically. If a minimum size is reached, it reverts to vertical translation.
    Stack(
     children: [
      body,   
      Sheet(
        initialExtent: 200,
        child: Container(color: Colors.blue[100]),
      ),
     ],
    )
  7. Replace routes with MaterialWithModalsPageRoute

    main

    To enable smooth route animations when using modals, replace your standard route implementations with MaterialWithModalsPageRoute using one of the following methods:

    1. Using Navigator.push:

    Navigator.of(context).push(MaterialWithModalsPageRoute(builder: (context) => Container()));

    2. Using onGenerateRoute:

    onGenerateRoute: (settings) {
      return MaterialWithModalsPageRoute(settings: settings, builder: (context) => Container());
    },

    3. Using pageRouteBuilder:

    pageRouteBuilder: <T>(RouteSettings settings, WidgetBuilder builder) => MaterialWithModalsPageRoute<T>(settings: settings, builder: builder)
  8. Sync scroll views with modal drag gestures

    main

    To ensure that a scroll view inside the bottom sheet stays in sync with the modal's drag-to-dismiss gesture, assign ModalScrollController.of(context) to the primary scroll view's controller property.

    showMaterialModalBottomSheet(
      context: context,
      builder: (context) => SingleChildScrollView(
        controller: ModalScrollController.of(context),
        child: Container(),
      ),
    );
  9. Add a bottom sheet to your page

    main

    To implement a bottom sheet, wrap your page content in a Stack and place the Sheet widget above the content you want to overlap. By default, the Sheet widget occupies all available space and manages user interactions with the sheet content.

    Note: Sheet uses Material theming by default. If you want to remove the Material theming entirely, use RawSheet instead.

    Scaffold(
        body: Stack(
            children: [
                body,
                Sheet(
                    child: MySheetContent()
                )
            ]
        )
    )
  10. Customize iOS launch screen assets

    main

    To customize the launch screen for the iOS version of your Flutter app, you can either replace the image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a more visual approach.

    Using Xcode:

    1. Open your Flutter project's iOS workspace using open ios/Runner.xcworkspace.
    2. In the Xcode Project Navigator, select Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog.
    open ios/Runner.xcworkspace