flutter showModalBottomSheet监听点击遮罩层关闭
时间: 2023-10-14 15:03:22 浏览: 157
要实现在点击底部弹框的遮罩层时不关闭弹框,可以通过设置ModalBottomSheet的背景点击回调来实现。具体方法是在showModalBottomSheet方法中传入一个builder参数,在builder中返回一个GestureDetector组件,并设置onTap属性为一个空函数。这样当点击底部弹框的遮罩层时,由于空函数没有具体的操作,弹框就不会关闭。下面是示例代码:
```
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return GestureDetector(
onTap: () {},
child: Container(
// 弹框内容
),
);
},
);
```
通过这种方式,你可以在弹框中进行一些操作,并手动触发关闭弹框。请注意,这个方法适用于Flutter中的ModalBottomSheet组件。同时,也可以使用Listener widget来监听原始触摸事件,并进行相应的处理。可以使用Listener的onPointerDown属性来监听手指按下事件,然后通过setState方法来改变弹框的状态。具体代码可以参考中的示例。另外,在Flutter中也可以直接使用showModalBottomSheet来自定义底部选择器,这样可以更灵活地实现你的需求。希望这些方法可以帮助到你。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Flutter中showModalBottomSheet点击内容区域自动关闭Dialog解决](https://blog.csdn.net/u012766656/article/details/130472170)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [Flutter技术点-showModalBottomSheet关闭](https://blog.csdn.net/u011511921/article/details/106625970)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [flutter 中监听滑动事件](https://download.csdn.net/download/weixin_38666300/14025486)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文