I am using the flutter package back_pressed (https://pub.dev/packages/back_pressed), I also tried implementing my own code similar to theirs which uses WillPopScope.
The back_pressed plugin is working fine all over my app, except for on 1 of my screens… this screen is not listening and is doing the default behavior for when the back button is pressed on Android.
@override
Widget build(BuildContext context) {
SetSizes();
return WillPopScope(
onWillPop: () { exit(0); },
child: Content(),
);
}
I have also tried this:
@override
Widget build(BuildContext context) {
SetSizes();
return OnBackPressed(child: Content(), perform: LeaveScreen);
}
void LeaveScreen() {
print("..........................Back pressed..........................");
var navIndex = Provider.of<AppData>(context, listen: false).navIndex;
navIndex.jumpToTab(1);
}
The OnBackPressed code is working everywhere else in my app, except for this screen.
2
Answers
The problem was caused because I was using a plugin for my bottom navigation, the navigation was loaded in a controller file, then the controller loaded the screens content from another file...
Issue was I needed to implement WillPopScope on the navigation controller file.
the problem is the
exit(0)
. here is my WillPopScope implementation which works in my whole app:you neet to use
async => false
, sinceonWillPop
which is{required Future<bool> Function()? onWillPop}
expects aFuture Bool
not anint
or something.Further explanation on
onWillPop
:Called to veto attempts by the user to dismiss the enclosing [ModalRoute].
If the callback returns a Future that resolves to false, the enclosing route will not be popped.