Created
April 7, 2023 22:23
-
-
Save justinmc/01fed18e1b8d0bf8eb93241198dcb3ba to your computer and use it in GitHub Desktop.
This gist illustrates that a WillPopScope widget inside of a nested Navigator widget has no effect.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:flutter/material.dart'; | |
| void main() => runApp(_MyApp()); | |
| class _MyApp extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| initialRoute: '/nested_navigator', | |
| routes: <String, WidgetBuilder>{ | |
| '/nested_navigator': (BuildContext context) => _NestedNavigatorsPage(), | |
| }, | |
| ); | |
| } | |
| } | |
| class _NestedNavigatorsPage extends StatefulWidget { | |
| @override | |
| State<_NestedNavigatorsPage> createState() => _NestedNavigatorsPageState(); | |
| } | |
| class _NestedNavigatorsPageState extends State<_NestedNavigatorsPage> { | |
| @override | |
| Widget build(BuildContext context) { | |
| // This WillPopScope will have an effect because it is outside of the nested | |
| // Navigator widget. Returning false here will prevent back gestures. | |
| return WillPopScope( | |
| onWillPop: () async { | |
| return true; | |
| }, | |
| child: Navigator( | |
| onGenerateRoute: (RouteSettings settings) { | |
| switch (settings.name) { | |
| case '/': | |
| return MaterialPageRoute( | |
| builder: (BuildContext context) { | |
| return Scaffold( | |
| appBar: AppBar( | |
| title: const Text('Nested Navigators Example'), | |
| ), | |
| body: Center( | |
| // This WillPopScope has no effect because it's inside of | |
| // the nested Navigator widget. | |
| child: WillPopScope( | |
| onWillPop: () async { | |
| return false; | |
| }, | |
| child: const Column( | |
| mainAxisAlignment: MainAxisAlignment.center, | |
| children: <Widget>[ | |
| Text('Nested Navigator Page'), | |
| Text('A WillPopScope here, inside of a nested Navigator, has no effect. A back gesture will succeed anyway.'), | |
| ], | |
| ), | |
| ), | |
| ), | |
| ); | |
| }, | |
| ); | |
| default: | |
| throw Exception('Invalid route: ${settings.name}'); | |
| } | |
| }, | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment