如何在FLUTTER中关闭没有按钮的警报对话框

时间:2020-03-19 14:51:54

标签: flutter

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

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()关闭警报对话框。 拜托,任何人,请给我一个提示,可以不使用按钮从外部关闭警报对话框。

1 个答案:

答案 0 :(得分: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);
                      }