如何消除颤振对话?

我是个新手,我想在完成任务后结束我的对话。我试过:

Navigator.pop(context, true);

但是我的屏幕变黑了,对话框还在上面。这是我的对话框代码。

Dialog _dialog = new Dialog(
child: new Row(
mainAxisSize: MainAsixSize.min,
children: <Widget> [
new CircularProgressIndicator(),
new Text("Loading")]),


);
126294 次浏览

https://docs.flutter.io/flutter/material/showDialog.html says

The dialog route created by this method is pushed to the root navigator. If the application has multiple Navigator objects, it may be necessary to call Navigator.of(context, rootNavigator: true).pop(result) to close the dialog rather just Navigator.pop(context, result).

so I'd assume one of these two should do what you want.

If you don't want to return any result after showDialog is closed, you can use

Navigator.pop(context);

If you want to pass result call

Navigator.pop(context, result);

Example:

showDialog(
context: context,
builder: (_) {
return AlertDialog(
title: Text('Wanna Exit?'),
actions: [
FlatButton(
onPressed: () => Navigator.pop(context, false), // passing false
child: Text('No'),
),
FlatButton(
onPressed: () => Navigator.pop(context, true), // passing true
child: Text('Yes'),
),
],
);
}).then((exit) {
if (exit == null) return;


if (exit) {
// user pressed Yes button
} else {
// user pressed No button
}
});

This code works for me:

  BuildContext dialogContext;
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
dialogContext = context;
return Dialog(
child: new Row(
mainAxisSize: MainAxisSize.min,
children: [
new CircularProgressIndicator(),
new Text("Loading"),
],
),
);
},
);


await _longOperation();
Navigator.pop(dialogContext);

This will close dialog/alert box

Navigator.of(context).pop();
//it work conrrectly
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},

Generally Navigator.pop(context); works.

But If the application has multiple Navigator objects and dialogBox doesn't close, then try this

Navigator.of(context, rootNavigator: true).pop();

If you want to pass result call, try

Navigator.pop(context,result);

OR

Navigator.of(context, rootNavigator: true).pop(result);

Better to use Completer, because if your operation is too short or the device is too slow, then the dialogContext variable will not be initialized and you can't close the dialog.

final dialogContextCompleter = Completer<BuildContext>();
showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext dialogContext) {
if(!dialogContextCompleter.isCompleted) {
dialogContextCompleter.complete(dialogContext);
}
return AlertDialog(
content: CircularProgressIndicator(),
);
},
);


// Close progress dialog
final dialogContext = await dialogContextCompleter.future;
Navigator.pop(dialogContext);
showDialog(
context: context,
builder: (BuildContext context) => Center(
child: CircularProgressIndicator(),
),
);


await Future<int>.delayed(Duration(seconds: 3));


Navigator.of(context, rootNavigator: true).pop();

Add on to Günter's answers. If you need to dismiss it when the user clicks elsewhere change this property to true

barrierDismissible: true,