如何防止 bottom sheet dismiss flutter

How to prevent bottom sheet dismiss flutter

我想防止在 flutter 中向下滑动时关闭 bottom sheet, 我想使用Scaffold.of(context).showBottomSheet<void>((BuildContext context) => ...)而不是showModalBottomSheet,因为我需要脚手架信息,请问showBottomSheet有什么解决方案吗?我该怎么做?

GestureDetector 包裹您的小部件并禁用拖动:

Scaffold.of(context).showBottomSheet(
  (context) => GestureDetector(
    child: YourWidget(),
    onVerticalDragStart: (_) {},
  ),
)
showModalBottomSheet(
    isDismissible: false,
)

尽管@Sami 的回答工作正常,但它不是很优雅,因为它似乎是一种解决方法。

对于这种情况,您应该使用 AbsorbPointer 而不是只使用空手势。

简单地说(粘贴文档):

A widget that absorbs pointers during hit testing.

在你的情况下,你会这样使用:

Scaffold.of(context).showBottomSheet(
  (context) => AbsorbPointer(child: Container()),
)

如果您使用 showModalBottomSheet,请使用 enableDrag 属性。

showModalBottomSheet<bool>(
        context: context,
        enableDrag: false,
        ...
        builder: (BuildContext bc) {
           return ..your widgets...
        }
);