【问题标题】:How to close an Alert dialog without a button in FLUTTER如何在 FLUTTER 中关闭没有按钮的警报对话框
【发布时间】:2020-06-30 17:35:11
【问题描述】:

我想在用户按下登录按钮时显示一个圆圈指示器。但有时可能会发生错误,例如错误的电子邮件或错误的密码。在这种情况下,应停止圆圈指示器并显示错误消息。圆形指示器警报对话框中没有按钮。我希望它在发现错误时自动关闭

onPressed: () async {
                      AuthResult user;
                      progressIndi();

                      try {
                        user = await _auth.signInWithEmailAndPassword(email: email, password: pass);
                      } catch (err) {
                        finderror(err.code);
                      }

finderror 功能会找到要显示的错误信息。 progressIndi() 函数将显示警报对话框。我试图用堆栈来实现它。但只有在我关闭对话框并再次按下登录按钮时才会显示更改。

void progressIndi() {
showDialog(
  barrierDismissible: false,
  context: context,
  builder: (BuildContext context) {
    return IndexedStack(
      index: isError,
      children: <Widget>[

        AlertDialog(
          content: new Row(
            children: [
              CircularProgressIndicator(),
              Container(
                  margin: EdgeInsets.only(left: 5),
                  child: Text(" Loading")),
            ],
          ),
        ),

        AlertDialog(
          title: Text("Error Found"),
          content: Text(errorMessage),
          actions: <Widget>[
            // usually buttons at the bottom of the dialog
            new FlatButton(
              child: new Text("Try Again"),
              onPressed: () {
                Navigator.of(context).pop();
                isError = 0;
              },
            ),
          ],
        ),
      ],
    );
  },
);

}

我知道可以使用按钮和 Navigator.of(context).pop() 关闭警报对话框。 任何人,请给我一个提示,让我在没有按钮的情况下从外部关闭警报对话框。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    当发生错误时,您可以通过在异常发生后调用 Navigator.of(context).pop() 来删除对话框,然后显示另一个错误对话框。希望这会有所帮助。

      AuthResult user;
                          progressIndi();
    
                          try {
                            user = await _auth.signInWithEmailAndPassword(email: email, password: pass);
                          } catch (err) {
    //Use pop here.
                            Navigator.of(context).pop();
    //make findError open another dialog.
                            finderror(err.code);
                          }
    

    【讨论】:

    • 是的,不会。由于您使用的是 barrierDismissable: false,因此您可以安全地调用 Navigator.of(context).pop() 而不会弹出当前页面但会弹出对话框。希望这可以帮助。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多