Flutter - How to dismiss a showDialog widget in the middle

元气小坏坏 提交于 2020-02-05 04:35:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!