Skip to content
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

[go_router] Fixes the Android back button ignores top level route's o… #4984

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
}