Reputation: 3849
I want to always execute a function when a screen is closed, either when a user presses the back button or does a swipe to close the page.
What I have tried is to use WillPopScope
return WillPopScope(
onWillPop: _onWillPop,
child: CupertinoScaffold( ... the rest of my tree
Future<bool> _onWillPop() async {
myFunction();
return true;
}
While this does successfully execute my function whenever the page is closed via the back button, it seems to have disabled being able to swipe to go back on iOS. Swipe to go back on android still seems to function.
Is there another way of doing this so I can retain the swipe to go back functionality on iOS?
Just incase it matters, myFunction()
involves a provider and context and throws an error when I try to call it from dispose
.
In actuality myFunction() is:
context.read(myVisibilityProvider).changeVisibility(false);
Upvotes: 1
Views: 2653
Reputation: 414
I successfully managed to run a function calling it from dispose
retaining the iOS swipe to back gesture.
class ScrondScreen extends StatefulWidget {
const ScrondScreen({Key? key}) : super(key: key);
@override
_ScrondScreenState createState() => _ScrondScreenState();
}
class _ScrondScreenState extends State<ScrondScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
);
}
void myFunction() {
print('I run');
}
@override
void dispose() {
myFunction();
super.dispose();
}
}
console:
flutter: I run
Upvotes: 3