ios - Pop in navigation doesn't dismiss the Dialog, but pop parent widget - Stack Overflow

admin2025-05-02  0

I push a Dialog and put a button on it to close the dialog using Navigator.of(context).pop() But when I press the button, the dialog does not close but the parent widget closes sequentially according to the widget tree. It seems that this only happens on iOS. video for you to visualize

In parent widget:

showDialog<bool>(
          context: context,
          builder: (ct) => MyDialog(
              )).then((v) {
                  if (v ?? false) {
                      myFunc();
                  }
               });

In MyDialog:

void close() {
   Navigator.of(context).pop(false);
}
  1. I tried adding rootNavigator: true as some tutorials.
  2. I tried passing an additional BuildContext to the Dialog:
showDialog<bool>(
          context: context,
          builder: (ct) => MyDialog(contx:xt
              )).then((v) {
                  if (v ?? false) {
                      myFunc();
                  }
               });

and pop :

void close() {
   Navigator.of(contx).pop(false);
}

Things seem to be getting better but the problem sometimes comes back.

I push a Dialog and put a button on it to close the dialog using Navigator.of(context).pop() But when I press the button, the dialog does not close but the parent widget closes sequentially according to the widget tree. It seems that this only happens on iOS. video for you to visualize

In parent widget:

showDialog<bool>(
          context: context,
          builder: (ct) => MyDialog(
              )).then((v) {
                  if (v ?? false) {
                      myFunc();
                  }
               });

In MyDialog:

void close() {
   Navigator.of(context).pop(false);
}
  1. I tried adding rootNavigator: true as some tutorials.
  2. I tried passing an additional BuildContext to the Dialog:
showDialog<bool>(
          context: context,
          builder: (ct) => MyDialog(contx:xt
              )).then((v) {
                  if (v ?? false) {
                      myFunc();
                  }
               });

and pop :

void close() {
   Navigator.of(contx).pop(false);
}

Things seem to be getting better but the problem sometimes comes back.

Share Improve this question asked Jan 2 at 12:07 Tuan AnhTuan Anh 211 bronze badge
Add a comment  | 

2 Answers 2

Reset to default 0

Give this approach a try and let me know if it works for your use case.

Add

useRootNavigator : true

in showDialog

showDialog<bool>(
 useRootNavigator : true,
      context: context,
      builder: (ct) => MyDialog(
          )).then((v) {
              if (v ?? false) {
                  myFunc();
              }
           });

You can try to pop dialog with the same BuildContext you opened it

Eg: using contextA to showDialog, then use Navigator.of(contextA).pop() to close it

Many BuildContext with the same name "context" may confuse usage

转载请注明原文地址:http://www.anycun.com/QandA/1746121451a91968.html