'Flutter "showDialog" with Navigator.pop()

I have problem with showDialog, when i press nothing happens but if i use Navigator.pushNamed(context, "/screen1") it works. I can not run Navigator.pop(context), it does not return any errors.

_showDialog(BuildContext context) {
return showDialog(
    context: context,
    builder: (BuildContext context) {
      return AlertDialog(
        title: new Text("Alert Dialog title"),
        actions: <Widget>[
          new FlatButton(
            child: new Text("Back"),
            onPressed: () {
              //Navigator.pushNamed(context, "/screen1");
              Navigator.pop(context);
            },
          ),
        ],
      );
    });}

In my build() :

IconButton(
iconSize: 30.0,
onPressed: () => _showDialog(context),
icon: Icon(
  Icons.clear,
  color: Colors.white,
 ),

)



Solution 1:[1]

had the same issue .having useRootNavigator: false, in showDialog params solved my issue .

Solution 2:[2]

Use pop() two times:-

Navigator.of(context).pop(); Navigator.of(context).pop();

Reason: first pop function dismiss the dialog and Second pop function close the screen

Solution 3:[3]

The above answer should be accepted for this question, just elaborating the above answer

return showDialog(
context: context,
useRootNavigator: false, //this property needs to be added 
builder: (BuildContext context) {....});

Solution 4:[4]

For closing dialogs, you can also use:

Navigator.pop(context, true);

Source: https://docs.flutter.io/flutter/widgets/Navigator/pop.html

Solution 5:[5]

For those who have nested/multiple Navigators, you can also use the pop method as shown below (notice the named parameter rootNavigator set to true):

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

As suggested by others, I tried setting the useRootNavigator in the showDialog as false but this way the barrierColor wasn't covering the whole screen (it was only covering the screen displayed by Navigator object the dialog was in).

In summary, you can use the way showed above if you have nested navigators and want to achieve having the barrierColor covering the whole screen.

Solution 6:[6]

First option: Create a custom theme based on your current theme and change the color of the relevant field

cp ~/.oh-my-zsh/themes/alanpeabody.zsh-theme ~/.oh-my-zsh/themes/alanpeabody-custom.zsh-theme

Change zsh terminal theme to your custom theme - edit ~/.zshrc and change ZSH_THEME="alanpeabody" to

ZSH_THEME="alanpeabody-custom"

Edit ~/.oh-my-zsh/themes/alanpeabody-custom.zsh-theme in order to change the color of the current working directory value, just change the blue value to one of the available color names: black, red, green, yellow, blue, magenta, cyan, white of the line below

local pwd='%{$fg[blue]%}%~%{$reset_color%}'

Second option: Copy ~/.oh-my-zsh/themes/alanpeabody.zsh-theme content to the bottom of your ~/.zshrc and edit the line mention above

Changes takes effect when opening a new terminal session or by running

source ~/.zshrc

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yusuf Fathi
Solution 2 Rituraj Shakti
Solution 3 Prashant
Solution 4 Rohan Taneja
Solution 5 satler
Solution 6 Eladio