如何在FLUTTER中关闭没有按钮的警报对话框 [英] How to close an Alert dialog without a button in FLUTTER
问题描述
我想在用户按下登录按钮时显示一个圆圈指示器.但是有时可能会发生错误,例如错误的电子邮件或错误的密码.在这种情况下,圆形指示器应停止并显示错误消息.圆圈指示器警报对话框中没有按钮.我希望它在发现错误时自动关闭
I want to show a circle indicator when the user presses the login button. But sometimes an error can occur such as a Wrong email or wrong password. In that case, the circle indicator should be stopped and the error message should be shown. There is no button in circle indicator alert dialog. I want it to automatically close when an error found
onPressed: () async {
AuthResult user;
progressIndi();
try {
user = await _auth.signInWithEmailAndPassword(email: email, password: pass);
} catch (err) {
finderror(err.code);
}
finderror 功能将找到要显示的错误消息. progressIndi()函数将显示警报对话框.我试图用堆栈来实现它.但是,只有当我关闭对话框并再次按下登录按钮时,它才会显示更改.
finderror function will find the error message to show. progressIndi() function will show the alert dialog. I tried to implement it with stacks. But it's only showing changes when I close the dialog box and press the login button again.
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()关闭警报对话框. 拜托,任何人,请给我一个提示,可以不使用按钮从外部关闭警报对话框.
I know that alert dialog can be closed using the button and Navigator.of(context).pop() . Please, anyone, give me a hint to close the alert dialog from outside without a button.
推荐答案
发生异常后,可以通过在异常发生后立即调用Navigator.of(context).pop()来删除对话框,然后显示另一个错误对话框.希望这会有所帮助.
You can remove the dialog when an error occurs by calling Navigator.of(context).pop() just after the exception occurs and then show another dialog for error. Hope this helps.
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);
}
这篇关于如何在FLUTTER中关闭没有按钮的警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!