flutter: how to handle back button press when using navigator with pages flutter: how to handle back button press when using navigator with pages dart dart

flutter: how to handle back button press when using navigator with pages


This is not possible with Navigator 2.0 without extending RouterDelegate. A system back button press is handled by RouteDelegate's popRoute method. According to the docs:

The method should return a boolean Future to indicate whether this delegate handles the request. Returning false will cause the entire app to be popped.

The default implementation always returns false. Therefore, you must override it to handle system back button presses.

UPDATE
I have published an article presenting a quick and easy-to-understand guide to using Navigator 2.0. You can find it here.


You'll have to use 'WillPopScope' https://api.flutter.dev/flutter/widgets/WillPopScope-class.html

Example

Future<bool> _willPopScopeCall() async {// code to show toast or modalreturn true; // return true to exit app or return false to cancel exit}//wrap your scaffold with WillPopScopeWillPopScope(child: new Scaffold(), onWillPop: _willPopScopeCall);