-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[go_router] Fixes StatefulShellRoute PopScope behavior to respect back button #11432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| changelog: | | ||
| - Fix StatefulShellRoute PopScope behavior to orchestrate back button pops to the root branch when inner navigators cannot pop. | ||
| version: patch |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| // Copyright 2013 The Flutter Authors | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:go_router/go_router.dart'; | ||
|
|
||
| import 'test_helpers.dart'; | ||
|
|
||
| void main() { | ||
| testWidgets( | ||
| 'PopScope in StatefulShellRoute branch works on subsequent visits', | ||
| (WidgetTester tester) async { | ||
| int tabBPopCount = 0; | ||
|
|
||
| final routes = <RouteBase>[ | ||
| StatefulShellRoute.indexedStack( | ||
| builder: | ||
| ( | ||
| BuildContext context, | ||
| GoRouterState state, | ||
| StatefulNavigationShell navigationShell, | ||
| ) { | ||
| return Scaffold( | ||
| body: navigationShell, | ||
| bottomNavigationBar: BottomNavigationBar( | ||
| currentIndex: navigationShell.currentIndex, | ||
| onTap: (int index) => navigationShell.goBranch(index), | ||
| items: const <BottomNavigationBarItem>[ | ||
| BottomNavigationBarItem( | ||
| icon: Icon(Icons.home), | ||
| label: 'A', | ||
| ), | ||
| BottomNavigationBarItem( | ||
| icon: Icon(Icons.business), | ||
| label: 'B', | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| }, | ||
| branches: <StatefulShellBranch>[ | ||
| StatefulShellBranch( | ||
| routes: <RouteBase>[ | ||
| GoRoute( | ||
| path: '/tabA', | ||
| builder: (BuildContext context, GoRouterState state) => | ||
| const DummyScreen(key: ValueKey<String>('tabA')), | ||
| ), | ||
| ], | ||
| ), | ||
| StatefulShellBranch( | ||
| routes: <RouteBase>[ | ||
| GoRoute( | ||
| path: '/tabB', | ||
| builder: (BuildContext context, GoRouterState state) { | ||
| return PopScope( | ||
| canPop: false, | ||
| onPopInvokedWithResult: (bool didPop, Object? result) { | ||
| tabBPopCount++; | ||
| if (!didPop) { | ||
| context.go('/tabA'); | ||
| } | ||
|
Comment on lines
+60
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation of To make this test more accurately reflect the scenario your fix is targeting, I suggest removing the navigation logic from the test's onPopInvokedWithResult: (bool didPop, Object? result) {
tabBPopCount++;
}, |
||
| }, | ||
| child: const DummyScreen(key: ValueKey<String>('tabB')), | ||
| ); | ||
| }, | ||
|
Comment on lines
+58
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test case includes an explicit |
||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ]; | ||
|
|
||
| final GoRouter router = await createRouter( | ||
| routes, | ||
| tester, | ||
| initialLocation: '/tabA', | ||
| ); | ||
|
|
||
| // 1. Visit Tab A | ||
| expect(find.byKey(const ValueKey<String>('tabA')), findsOneWidget); | ||
| expect(find.byKey(const ValueKey<String>('tabB')), findsNothing); | ||
|
|
||
| // 2. Switch to Tab B | ||
| router.go('/tabB'); | ||
| await tester.pumpAndSettle(); | ||
| expect(find.byKey(const ValueKey<String>('tabB')), findsOneWidget); | ||
|
|
||
| // 3. Press back button on Tab B (First Visit) | ||
| await simulateAndroidBackButton(tester); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| // Should have switched back to Tab A | ||
| expect(find.byKey(const ValueKey<String>('tabA')), findsOneWidget); | ||
| expect(tabBPopCount, 1); | ||
|
|
||
| // 4. Switch to Tab B again (Second Visit) | ||
| router.go('/tabB'); | ||
| await tester.pumpAndSettle(); | ||
| expect(find.byKey(const ValueKey<String>('tabB')), findsOneWidget); | ||
|
|
||
| // 5. Press back button on Tab B (Second Visit) | ||
| await simulateAndroidBackButton(tester); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| // Verify that the PopScope was triggered again | ||
| expect(tabBPopCount, 2); | ||
|
|
||
| // Should have switched back to Tab A again | ||
| expect(find.byKey(const ValueKey<String>('tabA')), findsOneWidget); | ||
| }, | ||
| ); | ||
|
|
||
| testWidgets( | ||
| 'StatefulShellRoute switches to root branch by default on back button', | ||
| (WidgetTester tester) async { | ||
| final routes = <RouteBase>[ | ||
| StatefulShellRoute.indexedStack( | ||
| builder: | ||
| ( | ||
| BuildContext context, | ||
| GoRouterState state, | ||
| StatefulNavigationShell navigationShell, | ||
| ) { | ||
| return Scaffold( | ||
| body: navigationShell, | ||
| bottomNavigationBar: BottomNavigationBar( | ||
| currentIndex: navigationShell.currentIndex, | ||
| onTap: (int index) => navigationShell.goBranch(index), | ||
| items: const <BottomNavigationBarItem>[ | ||
| BottomNavigationBarItem( | ||
| icon: Icon(Icons.home), | ||
| label: 'A', | ||
| ), | ||
| BottomNavigationBarItem( | ||
| icon: Icon(Icons.business), | ||
| label: 'B', | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| }, | ||
| branches: <StatefulShellBranch>[ | ||
| StatefulShellBranch( | ||
| routes: <RouteBase>[ | ||
| GoRoute( | ||
| path: '/tabA', | ||
| builder: (BuildContext context, GoRouterState state) => | ||
| const DummyScreen(key: ValueKey<String>('tabA')), | ||
| ), | ||
| ], | ||
| ), | ||
| StatefulShellBranch( | ||
| routes: <RouteBase>[ | ||
| GoRoute( | ||
| path: '/tabB', | ||
| builder: (BuildContext context, GoRouterState state) => | ||
| const DummyScreen(key: ValueKey<String>('tabB')), | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ]; | ||
|
|
||
| final GoRouter router = await createRouter( | ||
| routes, | ||
| tester, | ||
| initialLocation: '/tabA', | ||
| ); | ||
|
|
||
| expect(find.byKey(const ValueKey<String>('tabA')), findsOneWidget); | ||
|
|
||
| router.go('/tabB'); | ||
| await tester.pumpAndSettle(); | ||
| expect(find.byKey(const ValueKey<String>('tabB')), findsOneWidget); | ||
|
|
||
| await simulateAndroidBackButton(tester); | ||
| await tester.pumpAndSettle(); | ||
|
|
||
| expect(find.byKey(const ValueKey<String>('tabA')), findsOneWidget); | ||
| }, | ||
| ); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation of
PopScopemight interfere with nestedPopScopewidgets within a branch.If a child route on a non-root branch (e.g., Tab B) uses a
PopScopeto block a pop (for instance, to show a "Discard changes?" confirmation dialog), thedidPopvalue in this shell-levelPopScopewill befalse. Consequently,goBranch(0)will be triggered, switching the user to the root branch even though the intention was to stay on the current branch and handle the pop locally.To fix this, you should check if the current branch's navigator can actually pop before deciding to switch branches. If
navigator.canPop()is true butdidPopis false, it implies a nestedPopScopeblocked the pop, and the shell should not intervene.