Flutter-如何在中间关闭showDialog小部件 [英] Flutter - How to dismiss a showDialog widget in the middle
问题描述
我有两个同时运行的进程在相同的上下文中彼此独立运行.我们将其命名为A和B.两者都会弹出一个忙碌指示器.
I have two simultaneous processes run independently between each other on the same context. Let's name it A and B. Both popup a busy indicator.
showDialog(... child: Center(child: CircularProgressIndicator()) ...);
完成后,B将弹出一个AlertDialog
.有时,如果A花费的时间更长,则在A忙碌指示消失之前会弹出警报对话框.因此,当A完成时,它将关闭警报对话框而不是忙碌指示器.
On finish, B will popup an AlertDialog
. Sometimes if A take longer, the alert dialog popup before busy indicator of A is dismissed. So when A finished, it will dismiss the alert dialog not the busy indicator.
Navigator.of(context).pop(data);
如何选择要关闭的showDialog
小部件?
How to choose which showDialog
widget to dismiss?
推荐答案
也许可以这样工作:
final GlobalKey navigator1 = GlobalKey<NavigatorState>();
final GlobalKey navigator2 = GlobalKey<NavigatorState>();
...
_showDialog(context, 'alert1', navigator1);
_showDialog(context, 'alert2', navigator2);
...
_showDialog(BuildContext context, String text, GlobalKey key) {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
key: key,
title: new Text(text),
actions: <Widget>[
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.pop(key.currentContext);
},
),
],
);
});
}
为每个对话框提供一个导航键,然后使用该键关闭所需的对话框.
You give a navigator key for each dialog and use that key to dismiss the dialog you want.
希望有帮助
这篇关于Flutter-如何在中间关闭showDialog小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!