user3532201
user3532201

Reputation: 639

Swipe back gesture on iOS is overwritten by WillPopScope

If I am using WillPopScope to overwrite the BackButton behavior with a new route, it works fine. But on iOS, the automatic 'SwipeBack' gesture is not working any more. How can I set the SwipeBack gesture on iOS to push the current screen to the page with the class 'StartScreen'?

WillPopScope(onWillPop: (){
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (context) => StartScreen(),
        ),
      );
    }

Upvotes: 8

Views: 8266

Answers (2)

triple7
triple7

Reputation: 883

WillPopScope is not meant to affect navigation (I.e.: push a route on the navigator). It is meant for vetoing (preventing) navigation away from the current route when you don't want the user to navigate away. (For example, when it might result in data loss.)

Try CupertinoWillPopScope, as it allows you to conditionally veto the back navigation. So you can only block it when there's an actual need.

Upvotes: 3

Artem Krupp
Artem Krupp

Reputation: 91

This may be a very late answer, but at the moment there is no clear opportunity to listen to this gesture

You can achieve the call to onWillPop when you press the back button, and at the same time do not block the gesture if you make a descendant class of the ModalRoute class (or its descendants, such as MaterialPageRoute) and override the hasScopedWillPopCallback method (however, the gesture will not call onWillPop)

Upvotes: 2

Related Questions