6

I want to prevent dismissing the bottom sheet on swipe down in flutter, I want to use

Scaffold.of(context).showBottomSheet<void>((BuildContext context) => ...)

instead of showModalBottomSheet because I need to scaffold information, is there any solution for showBottomSheet? how can I do it?

Hamed
  • 5,867
  • 4
  • 32
  • 56
  • 1
    Does this answer your question? [Flutter : How to disable drag down to close showModalBottomSheet](https://stackoverflow.com/questions/60861634/flutter-how-to-disable-drag-down-to-close-showmodalbottomsheet) – AskNilesh Jul 16 '20 at 14:13

4 Answers4

10

If you are using showModalBottomSheet use enableDrag property.

showModalBottomSheet<bool>(
        context: context,
        enableDrag: false,
        ...
        builder: (BuildContext bc) {
           return ..your widgets...
        }
);
bikram
  • 7,127
  • 2
  • 51
  • 63
4
showModalBottomSheet(
    isDismissible: false,
)
Wali Khan
  • 586
  • 1
  • 5
  • 13
  • 2
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Jul 18 '20 at 10:04
  • 2
    Thank you, but I want to use `Scaffold.of(context).showBottomSheet((BuildContext context) => ...)` instead of `showModalBottomSheet` because I need to scaffold information, is there any solution for `showBottomSheet`? – Hamed Jul 19 '20 at 04:29
  • Perfect! lol i forgot this param name – Java Nerd Jan 11 '23 at 18:00
3

Wrap your widget with a GestureDetector and disable drag:

Scaffold.of(context).showBottomSheet(
  (context) => GestureDetector(
    child: YourWidget(),
    onVerticalDragStart: (_) {},
  ),
)
Sami Haddad
  • 1,356
  • 10
  • 15
  • Thank you, but I want to use `Scaffold.of(context).showBottomSheet((BuildContext context) => ...)` instead of `showModalBottomSheet` because I need to scaffold information, is there any solution for `showBottomSheet`? – Hamed Jul 19 '20 at 04:29
2

Even though @Sami answer works fine, it's not quite elegant, as it seems like a workaround.

For cases like this, you should use AbsorbPointer instead of just using an empty Gesture.

It's, simply put (pasting the docs):

A widget that absorbs pointers during hit testing.

In your case, you would use like this:

Scaffold.of(context).showBottomSheet(
  (context) => AbsorbPointer(child: Container()),
)
Guilherme Matuella
  • 2,193
  • 1
  • 17
  • 32