【问题标题】:Flutter prevent close dialog on back pressed颤振防止后按时关闭对话框
【发布时间】:2020-05-02 10:39:44
【问题描述】:

我正在使用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: () {
              },
            ),
          ],
        );
      },
    );

如何让它不关闭?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您需要将您的 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 参数,您可以在其中传递孩子弹出时想要的功能。在这种情况下,参数接收一个空函数,因此它不会弹出。

    【讨论】:

    • 谢谢,我试过这样,但对话一直关闭
    • 算了,我的代码中还有一个错误哈哈,非常好用谢谢!
    【解决方案2】:

    您需要使用WillPopScope。它将使用onWillPop 上的函数来确定对话框是否关闭。在这种情况下总是假的,所以用户不能使用后退按钮来关闭对话框。

    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) {
        // return object of type Dialog
        return WillPopScope(
          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: () {
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
          onWillPop: () async {
            return false;
          },
        );
      },
    );
    

    【讨论】:

      【解决方案3】:

      您也可以通过在WillPopScope 中设置onWillPop: () =&gt; Future.value(false) 来实现。现在onWillPop:(){} 如已接受答案中所述给出警告。

      警告信息: 此函数的返回类型为“Future”,但不以 return 语句结束。 尝试添加 return 语句,或将返回类型更改为“void”。

      所以,请改用onWillPop: () =&gt; Future.value(false)onWillPop: () async {return false;},

      showDialog(
        barrierDismissible: false,
        context: context,
        builder: (BuildContext context) {
          return WillPopScope(
            child: AlertDialog(
              ................
            ),
            onWillPop: () => Future.value(false),
          );
        },
      );
      

      【讨论】:

        猜你喜欢
        • 2012-08-22
        • 2020-06-02
        • 2018-11-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-20
        • 1970-01-01
        相关资源
        最近更新 更多