Skip to content

Commit 619af75

Browse files
authored
[go_router] Fixes the Android back button ignores top level route's o… (#4984)
�nExit. fixes flutter/flutter#135094
1 parent de67c87 commit 619af75

File tree

4 files changed

+88
-1
lines changed

4 files changed

+88
-1
lines changed

packages/go_router/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 11.0.1
2+
3+
- Fixes the Android back button ignores top level route's onExit.
4+
15
## 11.0.0
26

37
- Fixes the GoRouter.goBranch so that it doesn't reset extra to null if extra is not serializable.

packages/go_router/lib/src/delegate.dart

+6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
6262
return true;
6363
}
6464
}
65+
// This should be the only place where the last GoRoute exit the screen.
66+
final GoRoute lastRoute =
67+
currentConfiguration.matches.last.route as GoRoute;
68+
if (lastRoute.onExit != null && navigatorKey.currentContext != null) {
69+
return !(await lastRoute.onExit!(navigatorKey.currentContext!));
70+
}
6571
return false;
6672
}
6773

packages/go_router/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: go_router
22
description: A declarative router for Flutter based on Navigation 2 supporting
33
deep linking, data-driven routes and more
4-
version: 11.0.0
4+
version: 11.0.1
55
repository: https://github.com/flutter/packages/tree/main/packages/go_router
66
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22
77

packages/go_router/test/on_exit_test.dart

+77
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,81 @@ void main() {
166166
await tester.pumpAndSettle();
167167
expect(find.byKey(home), findsOneWidget);
168168
});
169+
170+
testWidgets('android back button respects the last route.',
171+
(WidgetTester tester) async {
172+
bool allow = false;
173+
final UniqueKey home = UniqueKey();
174+
final List<GoRoute> routes = <GoRoute>[
175+
GoRoute(
176+
path: '/',
177+
builder: (BuildContext context, GoRouterState state) =>
178+
DummyScreen(key: home),
179+
onExit: (BuildContext context) {
180+
return allow;
181+
},
182+
),
183+
];
184+
185+
final GoRouter router = await createRouter(routes, tester);
186+
expect(find.byKey(home), findsOneWidget);
187+
188+
// Not allow system pop.
189+
expect(await router.routerDelegate.popRoute(), true);
190+
191+
allow = true;
192+
expect(await router.routerDelegate.popRoute(), false);
193+
});
194+
195+
testWidgets('android back button respects the last route. async',
196+
(WidgetTester tester) async {
197+
bool allow = false;
198+
final UniqueKey home = UniqueKey();
199+
final List<GoRoute> routes = <GoRoute>[
200+
GoRoute(
201+
path: '/',
202+
builder: (BuildContext context, GoRouterState state) =>
203+
DummyScreen(key: home),
204+
onExit: (BuildContext context) async {
205+
return allow;
206+
},
207+
),
208+
];
209+
210+
final GoRouter router = await createRouter(routes, tester);
211+
expect(find.byKey(home), findsOneWidget);
212+
213+
// Not allow system pop.
214+
expect(await router.routerDelegate.popRoute(), true);
215+
216+
allow = true;
217+
expect(await router.routerDelegate.popRoute(), false);
218+
});
219+
220+
testWidgets('android back button respects the last route with shell route.',
221+
(WidgetTester tester) async {
222+
bool allow = false;
223+
final UniqueKey home = UniqueKey();
224+
final List<RouteBase> routes = <RouteBase>[
225+
ShellRoute(builder: (_, __, Widget child) => child, routes: <RouteBase>[
226+
GoRoute(
227+
path: '/',
228+
builder: (BuildContext context, GoRouterState state) =>
229+
DummyScreen(key: home),
230+
onExit: (BuildContext context) {
231+
return allow;
232+
},
233+
),
234+
])
235+
];
236+
237+
final GoRouter router = await createRouter(routes, tester);
238+
expect(find.byKey(home), findsOneWidget);
239+
240+
// Not allow system pop.
241+
expect(await router.routerDelegate.popRoute(), true);
242+
243+
allow = true;
244+
expect(await router.routerDelegate.popRoute(), false);
245+
});
169246
}

0 commit comments

Comments
 (0)