【问题标题】:How to prevent AlertDialog from not closing by clicking outside?如何防止AlertDialog不通过点击外部关闭?
【发布时间】:2018-11-11 02:55:22
【问题描述】:

我构建了一个 AlertDialog 来在我验证用户时显示加载,当它完成时我弹出它。

Widget loadingDialog = new AlertDialog(
content: new Row(
  children: <Widget>[
    new CircularProgressIndicator(),
    Padding(
      padding: const EdgeInsets.only(left: 8.0),
      child: new Text("Loading..."),
    ),
  ],
),);

但是,如果用户在对话框之外点击它会关闭。因此,当身份验证完成时,它仍然会弹出一些东西(我猜是脚手架),从而破坏了应用程序。 如何使对话框不可关闭?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    为防止在按下后退按钮时关闭对话框,您还应该将 AlertDialog(或任何其他小部件)包装在 WillPopScope 中。

    showDialog(
      context: context,
      barrierDismissible: false, // <-- Set this to false.
      builder: (_) => WillPopScope(
        onWillPop: () async => false, // <-- Prevents dialog dismiss on press of back button.
        child: AlertDialog(...),
      ),
    );
    

    【讨论】:

      【解决方案2】:

      // 用户定义函数 无效 _showloding() {

      // flutter defined function
      showDialog(
        barrierDismissible: false, // JUST MENTION THIS LINE
        context: context,
        builder: (BuildContext context) {
          // return object of type Dialog
          return AlertDialog(
            content: Padding(
              padding: const EdgeInsets.all(20.0),
              child: Container(height: 100,width: 100,child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Center(child: SizedBox(height: 50,width: 50,child: CircularProgressIndicator())),
                  Padding(
                    padding: const EdgeInsets.only(top: 20),
                    child: Center(child: Text("Processing")),
                  )
                ],
              )),
            )
          );
        },
      );
      

      }

      【讨论】:

        【解决方案3】:

        showDialog 内部有一个名为barrierDismissible 的属性。将此值设置为 false 将使您的 AlertDialog 无法通过单击外部来关闭。

        showDialog(
           ...
           barrierDismissible: false,
           ...
        

        【讨论】:

        • 不错。棘手的部分是,出于某种原因,我认为该属性将成为AlertDialog 的一部分(可能是Android 习惯)。不过,这样更有意义。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-16
        • 1970-01-01
        相关资源
        最近更新 更多