How to dismiss flutter dialog?
DialogFlutterDialog Problem Overview
I am new to flutter, I want to dismiss my dialog after the task completion. I've tried with:
Navigator.pop(context, true);
But my screen is getting black and dialog is still up there. here is my dialog code.
Dialog _dialog = new Dialog(
child: new Row(
mainAxisSize: MainAsixSize.min,
children: <Widget> [
new CircularProgressIndicator(),
new Text("Loading")]),
);
Dialog Solutions
Solution 1 - Dialog
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.
Solution 2 - Dialog
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);
Solution 3 - Dialog
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
}
});
Solution 4 - Dialog
//it work conrrectly
onPressed: () {
Navigator.of(context, rootNavigator: true).pop();
},
Solution 5 - Dialog
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);
Solution 6 - Dialog
This will close dialog/alert box
Navigator.of(context).pop();
Solution 7 - Dialog
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);