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);
}
- I tried adding
rootNavigator: true
as some tutorials. - 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.
2
Answers
Give this approach a try and let me know if it works for your use case.
Add
in showDialog
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