I am trying to display an overlay that closes when the native back button is pressed (or activated by gesture).
I have a PopScope widget inside an OverlayEntry which onPopInvoked method is not being called when supposed to, but the pop event is actually happening and detected by other parent PopScopes.
showOverlay() {
late OverlayEntry entry;
entry = OverlayEntry(
Material( // Also tried with scaffold and with PopScope as parent instead of child
child: PopScope(
onPopInvoked( // This function is never called
print('Flutter is awesome');
entry.remove();
),
child: Text('Overlay content')
),
),
);
}
// Obviously the actual view is bigger, tried to simplify the most
// and kept the widgets I thought were relevant or could have some effect on the overlay
Widget build(context) {
return FutureBuilder<MyObject>(
future: futureFunction,
builder: (context, snashot) {
return PopScope( // This onPopInvoked is being listened
canPop: false, // Avoids app close, isn't blocking any child PopScope (tried and they listen)
child: Material(
child: Stack(
TextButton(
onPressed: () {
showOverlay();
}
child: Text('Show overlay'),
),
),
),
);
}
);
}
Not sure if the nested navigation has something to do with this but I have read about some problems with it, so I include it.
The problematic widget has been navigated from a splash screen, navigation being handled by this main widget. (the one that goes in runApp(MainApp()))
Route<dynamic> onGenerateRoutes(RouteSettings s) {
return MaterialPageRoute<dynamic>(builder: (context) => ProblematicWidget(), settings: s);
}
Widget build(context) {
return MaterialApp(
onGenerateRoute:
builder: (context, child) {
return Overlay(
initialEntries: [
OverlayEntry(
builder: (context) {
return child ?? SplashSccreen();
}
),
]
);
}
);
}
I have tried in a simple project what i want and it worked but in the real project it is not working. In the real project I tried putting the PopScope in a reasonable position in the widgets tree, then tried in any possible level to check where it works and it is definitely the OverlayEntry that blocks, or at least I know that any PopScope inside it wont listen the pop event. Tried showDialog as alternative and worked but I need it to use overlay.
Help please I’m frustrated.