Skip to content

Disable Swipe to Navigate Back on iOS with WillPopScope(Flutter)

Flutter, Widgets, iOS1 min read

I've been working on a project and updating it on almost a weekly basis for a few months now. I was really surprised the other day when I realized that by default Flutter will automatically pop your routes on iOS when you swipe back. I was completely unaware of this behaviour for months and had been designing all of my screens so they had buttons to navigate backwards. Needless to say this was a pretty glaring bug in my eyes(and it says alot about my subpar pre-release testing process...), so I went searching for a solution to disable swipe navigation. What I found was a widget called WillPopScope.

So I'll just get straight into it. If you wrap the parent widget for your screen in WillPopScope and return false for the onWillPop prop it will disable the above-mentioned functionality and users won't be able to swipe back to navigate for that screen anymore. Here's an example of what I'm talking about below.

2 Widget build(BuildContext context) {
3 return WillPopScope(
4 child: Scaffold(
5 body: Container(
6 child: ...
7 ),
8 ),
9 onWillPop: () async => false,
10 );
11 }

The weird thing about the above fix is that changing onWillPop from false to true doesn't seem to revert to the previous behaviour. You can read more about this here if you're trying to determine behaviour dynamically. I'm also not sure what consequences this will have on Android in regards to the back button.