【问题标题】:Prevent to hide showModalBottomSheet from outside touch防止从外部触摸隐藏 showModalBottomSheet
【发布时间】:2019-12-27 14:48:13
【问题描述】:

是否可以防止ModalBottomSheet 被外界触摸隐藏?就像在 showDialog() 中一样,我们可以使用 barrierDismissible property to prevent dialog from closing on outside touch

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    你可以像这样使用isDismissible: false和enableDrag: false

    showModalBottomSheet(
          isDismissible: false,
          enableDrag: false,
    
          builder: (context) {
            return Container(
             height: 100.0
           )
          }
      );
    

    【讨论】:

      【解决方案2】:

      尝试在 showModalBottomSheet 中将 isDismissible 设置为 false

              showModalBottomSheet(
                          isDismissible: false,
                          context: context,
                          builder: (BuildContext bc) {
                            return SheetWidget();
                          },
                        );
      

      SheetWidget 是您尝试调用为 BottomSheet 的小部件

      【讨论】:

        【解决方案3】:
            class MyHomePage extends StatefulWidget {
            @override
            _MyHomePageState createState() => _MyHomePageState();
            }
        
            class _MyHomePageState extends State<MyHomePage> {
            final _scaffoldKey = new GlobalKey<ScaffoldState>();
            VoidCallback _showPersBottomSheetCallback;
        
            @override
            void initState() {
                super.initState();
                _showPersBottomSheetCallback = _showBottomSheet;
            }
        
            void _showBottomSheet() {
                setState(() {
                _showPersBottomSheetCallback = null;
                });
        
                _scaffoldKey.currentState
                    .showBottomSheet((context) {
                    return new Container(
                        color: Colors.greenAccent,
                        height: 300.0,
                        child: new Center(
                        child: new Text("Hi BottomSheet"),
                        ),
                    );
                    })
                    .closed
                    .whenComplete(() {
                    if (mounted) {
                        setState(() {
                        _showPersBottomSheetCallback = _showBottomSheet;
                        });
                    }
                    });
            }
        
            @override
            Widget build(BuildContext context) {
                return new Scaffold(
                key: _scaffoldKey,
                appBar: new AppBar(
                    title: new Text("Flutter BottomSheet"),
                ),
                body: GestureDetector(
                    behavior: HitTestBehavior.opaque,
                    onTap: () {},
                    child: Padding(
                    padding: const EdgeInsets.only(top: 10.0),
                    child: new Center(
                        child: new Column(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: <Widget>[
                            new RaisedButton(
                            onPressed: _showPersBottomSheetCallback,
                            child: new Text("Persistent"),
                            ),
                        ],
                        ),
                    ),
                    ),
                ),
                );
            }
            }
        

        【讨论】:

        • 当您已经有专用的showBottomSheet() 回调时,我认为这是一种解决方法而不是解决方案。
        【解决方案4】:

        您需要使用不包含屏障的showBottomSheet() 而不是使用showModalBottomSheet()。

        更多信息here

        【讨论】:

        • 对不起,我知道 ``showBottomSheet 但我正在寻找 showModalBottomSheet()。
        • showModalBottomSheet 和showBottomSheet 之间的唯一区别是后者不能通过触摸其上下文之外的内容来消除。和你要求的不一样吗?
        • 是的,但我认为我们可以在打开 showBottomSheet 后访问/触摸外部小部件?如果我错了,请纠正我?
        • 你是对的,你可以访问外部小部件,我可以知道你在找什么型号吗?您只是想防止在打开工作表时点击那些外部小部件?对于这种情况,您可以使用 AbsorbPointer 小部件。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-06-28
        • 2017-02-18
        • 1970-01-01
        • 1970-01-01
        • 2020-04-11
        • 2019-03-13
        • 2023-03-31
        相关资源
        最近更新 更多