Flutter 防止在按下后关闭对话框

标签 flutter

我正在使用showDialog函数来构建一个对话框,但是我需要避免当用户按下后退按钮时,对话框没有关闭,这是我的代码:

showDialog(
      barrierDismissible: false,
      context: context,

      builder: (BuildContext context) {
        // return object of type Dialog
        return AlertDialog(
          title: new Text("Hello"),
          content: new SingleChildScrollView(
            child: Container(),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog

            new FlatButton(
              child: new Text("Close"),
              onPressed: () {
              },
            ),
          ],
        );
      },
    );

我怎样才能让它不关闭?

最佳答案

您需要将 AlertDialon 附在 WillPopScope 上像这样:

showDialog(
      barrierDismissible: false,
      context: context,

      builder: (BuildContext context) {
        // return object of type Dialog
        return WillPopScope(
            onWillPop: (){},
            child:AlertDialog(
            title: new Text("Hello"),
            content: new SingleChildScrollView(
              child: Container(),),
            actions: <Widget>[
              // usually buttons at the bottom of the dialog
              new FlatButton(
                child: new Text("Close"),
                onPressed: () {
                },
              ),
            ],
          )
        )
      },
    );
WillPopScope为您提供 onWillPop参数,您可以在其中传递 child 弹出时所需的函数。在这种情况下,参数接收一个空函数,因此它不会弹出。

关于Flutter 防止在按下后关闭对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59755211/

相关文章:

flutter - DraggableScrollableSheet 在滚动前吸附到顶部

listview - 下拉 ListView

android-studio - 如何在Flutter中从应用名称中删除下划线

flutter - 如何在SingleChildScrollView中实现非滚动 ListView

flutter - 在 flutter 中组合使用 3 个输入值时出现问题

flutter - 为什么我的状态栏图标是黑色的,为什么我不能改变它? (Flutter 2.0 升级后)

canvas - 没有中心点的 Flutter drawArc

flutter - 从网络 flutter 中加载图像的更好方法

animation - Flutter - 从父类或另一个子类调用子类的动画函数

flutter - 如何在谷歌地图 Flutter 中动态绘制多边形