Skip to content

Commit

Permalink
[go_router] Fixes the Android back button ignores top level route's o… (
Browse files Browse the repository at this point in the history
flutter#4984)

�nExit.

fixes flutter#135094
  • Loading branch information
chunhtai authored Sep 25, 2023
1 parent de67c87 commit 619af75
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 11.0.1

- Fixes the Android back button ignores top level route's onExit.

## 11.0.0

- Fixes the GoRouter.goBranch so that it doesn't reset extra to null if extra is not serializable.
Expand Down
6 changes: 6 additions & 0 deletions packages/go_router/lib/src/delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class GoRouterDelegate extends RouterDelegate<RouteMatchList>
return true;
}
}
// This should be the only place where the last GoRoute exit the screen.
final GoRoute lastRoute =
currentConfiguration.matches.last.route as GoRoute;
if (lastRoute.onExit != null && navigatorKey.currentContext != null) {
return !(await lastRoute.onExit!(navigatorKey.currentContext!));
}
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
version: 11.0.0
version: 11.0.1
repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22

Expand Down
77 changes: 77 additions & 0 deletions packages/go_router/test/on_exit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,81 @@ void main() {
await tester.pumpAndSettle();
expect(find.byKey(home), findsOneWidget);
});

testWidgets('android back button respects the last route.',
(WidgetTester tester) async {
bool allow = false;
final UniqueKey home = UniqueKey();
final List<GoRoute> routes = <GoRoute>[
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) =>
DummyScreen(key: home),
onExit: (BuildContext context) {
return allow;
},
),
];

final GoRouter router = await createRouter(routes, tester);
expect(find.byKey(home), findsOneWidget);

// Not allow system pop.
expect(await router.routerDelegate.popRoute(), true);

allow = true;
expect(await router.routerDelegate.popRoute(), false);
});

testWidgets('android back button respects the last route. async',
(WidgetTester tester) async {
bool allow = false;
final UniqueKey home = UniqueKey();
final List<GoRoute> routes = <GoRoute>[
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) =>
DummyScreen(key: home),
onExit: (BuildContext context) async {
return allow;
},
),
];

final GoRouter router = await createRouter(routes, tester);
expect(find.byKey(home), findsOneWidget);

// Not allow system pop.
expect(await router.routerDelegate.popRoute(), true);

allow = true;
expect(await router.routerDelegate.popRoute(), false);
});

testWidgets('android back button respects the last route with shell route.',
(WidgetTester tester) async {
bool allow = false;
final UniqueKey home = UniqueKey();
final List<RouteBase> routes = <RouteBase>[
ShellRoute(builder: (_, __, Widget child) => child, routes: <RouteBase>[
GoRoute(
path: '/',
builder: (BuildContext context, GoRouterState state) =>
DummyScreen(key: home),
onExit: (BuildContext context) {
return allow;
},
),
])
];

final GoRouter router = await createRouter(routes, tester);
expect(find.byKey(home), findsOneWidget);

// Not allow system pop.
expect(await router.routerDelegate.popRoute(), true);

allow = true;
expect(await router.routerDelegate.popRoute(), false);
});
}

0 comments on commit 619af75

Please sign in to comment.