问题
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()) ...);
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);
How to choose which showDialog
widget to dismiss?
回答1:
May be something like this may work:
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.
Hope it helps
来源:https://stackoverflow.com/questions/59590939/flutter-how-to-dismiss-a-showdialog-widget-in-the-middle