How to dismiss flutter dialog?

DialogFlutter

Dialog 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);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmmy KangView Question on Stackoverflow
Solution 1 - DialogGünter ZöchbauerView Answer on Stackoverflow
Solution 2 - Dialoglive-loveView Answer on Stackoverflow
Solution 3 - DialogCopsOnRoadView Answer on Stackoverflow
Solution 4 - DialogWai Yan MoungView Answer on Stackoverflow
Solution 5 - DialogVicky SalunkheView Answer on Stackoverflow
Solution 6 - DialogKrishnamoorthy AcharyaView Answer on Stackoverflow
Solution 7 - DialogMihail YaView Answer on Stackoverflow