对话框未关闭

时间:2020-01-09 08:41:55

标签: android ios flutter

单击alertdialog的“确定”按钮后,我有一个AlertDialog,我正在从服务器获取数据。直到接收到数据,我才显示另一个加载对话框。但是一旦收到数据,加载对话框就不会关闭。

这是我的代码:

 final GlobalKey<State> _keyLoader = new GlobalKey<State>();


 Future<void> _handleSubmit(BuildContext context,id) async {
showDialog(
    context: context,
    builder: (BuildContext dialogcontext) {
      return AlertDialog(
        title: Text(
          'Topup',
        ),
        content: Text('Are you sure?'),
        actions: <Widget>[
          new FlatButton(
            child: new Text('Ok'),
            onPressed: () async {
              Navigator.of(dialogcontext).pop();
              _submit(dialogcontext, id);
            },
          ),
          new FlatButton(
            child: new Text('Cancel'),
            onPressed: () {
              Navigator.of(dialogcontext).pop();
            },
          )
        ],
      );
    });
  }

 Future<void> _submit(BuildContext context, int id) async {
try {
  Dialogs.showLoadingDialog(
      context, _keyLoader, "Please Wait..."); //invoking login
  await getData(id, context);
  Navigator.of(_keyLoader.currentContext, rootNavigator: true)
      .pop(); //close the dialoge

} catch (error) {
  print(error);
}
}

这是我的Dialogs类,用于显示加载对话框

 class Dialogs {
  static Future<void> showLoadingDialog(
  BuildContext context, GlobalKey key, String textToShow) async {
  return showDialog<void>(
    context: context,
    barrierDismissible: false,
    builder: (BuildContext context) {
      return new WillPopScope(
          onWillPop: () async => false,
          child: SimpleDialog(key: key,
              // backgroundColor: Colors.black54,
              children: <Widget>[
                Center(
                  child: Column(children: [
                    CircularProgressIndicator(
                      //backgroundColor: Colors.purple,
                      valueColor: new AlwaysStoppedAnimation<Color>(
                          Colors.pinkAccent[400]),
                    ),
                    SizedBox(
                      height: 10,
                    ),
                    Text(
                      textToShow,
                      style: TextStyle(color: Colors.pinkAccent[400]),
                    )
                  ]),
                )
              ]));
    });
  }
}

我也在日志中得到:

 Looking up a deactivated widget's ancestor is unsafe.
 I/flutter (21400): At this point the state of the widget's element tree is no longer stable.
 I/flutter (21400): To safely refer to a widget's ancestor in its dispose() method, save a 
 reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's 
 didChangeDependencies() method.

1 个答案:

答案 0 :(得分:1)

尝试一下,阅读TODO条评论

Future<void> _handleSubmit(BuildContext context, id) async {
  showDialog(
    context: context,
    builder: (BuildContext dialogContext) {
      return AlertDialog(
        title: Text('Topup'),
        content: Text('Are you sure?'),
        actions: <Widget>[
          new FlatButton(
            child: new Text('OK'),
            onPressed: () async {
              Navigator.of(dialogContext).pop();
              _submit(context, id); //TODO: Don't send dialog context since we already did pop on it
            },
          ),
          new FlatButton(
            child: new Text('Cancel'),
            onPressed: () {
              Navigator.of(dialogContext).pop();
            },
          )
        ],
      );
    },
  );
}

Future<void> _submit(BuildContext context, int id) async {
  try {
    Dialogs.showLoadingDialog(
        context, _keyLoader, "Please Wait..."); //invoking login
    await getData(id, context);
    await Future.delayed(Duration.zero); //TODO: Add this line
    Navigator.of(context).pop(); //TODO: Use the received context
  } catch (error) {
    print(error);
  }
}