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

[GoRouter] option to override initial route set by platform #4717

Merged
merged 34 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1f2a25b
feat: override platform route
opxdelwin Aug 15, 2023
a334fae
feat: override platform route
opxdelwin Aug 15, 2023
0f1e823
Update CHANGELOG.md
opxdelwin Aug 15, 2023
9ea3161
Merge branch 'main' of https://github.com/flutter/packages into b132557
opxdelwin Aug 15, 2023
e5d4e77
Merge branch 'b132557' of https://github.com/opxdelwin/packages into …
opxdelwin Aug 15, 2023
4519e8f
Removed unused imports
opxdelwin Aug 16, 2023
eb3d6a2
Code reformat using `dart format`
opxdelwin Aug 16, 2023
4af6234
Update packages/go_router/CHANGELOG.md
opxdelwin Aug 19, 2023
253a96f
Update packages/go_router/lib/src/router.dart
opxdelwin Aug 19, 2023
24e05dd
Implement suggest documentation updates
opxdelwin Aug 19, 2023
11b4782
Merge branch 'main' of https://github.com/flutter/packages into b132557
opxdelwin Aug 19, 2023
44cf082
Merge branch 'b132557' of https://github.com/opxdelwin/packages into …
opxdelwin Aug 19, 2023
ac93687
Merge branch 'main' of https://github.com/flutter/packages into b132557
opxdelwin Aug 22, 2023
6d71142
Merge branch 'main' of https://github.com/flutter/packages into b132557
opxdelwin Sep 16, 2023
2491fbd
add routing test
opxdelwin Sep 16, 2023
d1c8ece
dart format + CHANGELOG fix
opxdelwin Sep 16, 2023
6b56bf6
Merge branch 'main' into b132557
opxdelwin Sep 21, 2023
7ae8d51
Merge branch 'main' of https://github.com/flutter/packages into b132557
opxdelwin Sep 22, 2023
f05b37e
update docs and modify test
opxdelwin Sep 22, 2023
67cbd8b
add overridePlatformDefaultLocation to helpers
opxdelwin Sep 22, 2023
5a78754
Merge branch 'b132557' of https://github.com/opxdelwin/packages into …
opxdelwin Sep 22, 2023
49a495a
fix typo in comments
opxdelwin Sep 22, 2023
b7e5dac
restructure to improve memory usage
opxdelwin Sep 22, 2023
490e3da
docs++
opxdelwin Sep 22, 2023
1fc46e3
++
opxdelwin Sep 22, 2023
c37fbd7
++
opxdelwin Sep 22, 2023
e11df2a
Merge branch 'main' into b132557
opxdelwin Sep 22, 2023
1741047
Merge branch 'main' of https://github.com/flutter/packages into b132557
opxdelwin Sep 22, 2023
c6dee8f
Merge branch 'b132557' of https://github.com/opxdelwin/packages into …
opxdelwin Sep 22, 2023
9aca1d6
dart format
opxdelwin Sep 22, 2023
13fa394
Merge branch 'main' into b132557
opxdelwin Sep 23, 2023
84eba32
Merge branch 'main' into b132557
opxdelwin Sep 24, 2023
b28a513
Merge branch 'main' into b132557
chunhtai Sep 25, 2023
8bea7b6
Merge branch 'main' into b132557
chunhtai Sep 26, 2023
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 @@
## 10.2.1

- Adds optional parameter `overridePlatformDefaultLocation` to override initial route set by platform.

## 10.2.0

- Adds `onExit` to GoRoute.
Expand Down
17 changes: 17 additions & 0 deletions packages/go_router/lib/src/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class GoRouter implements RouterConfig<RouteMatchList> {
int redirectLimit = 5,
bool routerNeglect = false,
String? initialLocation,
this.overridePlatformDefaultLocation = false,
Object? initialExtra,
List<NavigatorObserver>? observers,
bool debugLogDiagnostics = false,
Expand All @@ -91,6 +92,8 @@ class GoRouter implements RouterConfig<RouteMatchList> {
initialExtra == null || initialLocation != null,
'initialLocation must be set in order to use initialExtra',
),
assert(!overridePlatformDefaultLocation || initialLocation != null,
'Initial location must be set to override platform default'),
assert(
(onException == null ? 0 : 1) +
(errorPageBuilder == null ? 0 : 1) +
Expand Down Expand Up @@ -299,6 +302,15 @@ class GoRouter implements RouterConfig<RouteMatchList> {
@override
late final GoRouteInformationParser routeInformationParser;

/// When set to [true], the route returned by `getInitialRoute` will take precedence over the
opxdelwin marked this conversation as resolved.
Show resolved Hide resolved
/// platform's default initial location. This allows developers to control the starting route
/// of the application independently of the platform.
///
opxdelwin marked this conversation as resolved.
Show resolved Hide resolved
/// Setting this parameter to [false] (default) will allow the platform's default initial
/// location to be used. It's advisable to only set this to true if you explicitly want to
opxdelwin marked this conversation as resolved.
Show resolved Hide resolved
/// ignore the platform's default initial location.
final bool overridePlatformDefaultLocation;

/// Returns `true` if there is at least two or more route can be pop.
bool canPop() => routerDelegate.canPop();

Expand Down Expand Up @@ -509,6 +521,11 @@ class GoRouter implements RouterConfig<RouteMatchList> {
String _effectiveInitialLocation(String? initialLocation) {
final String platformDefault =
WidgetsBinding.instance.platformDispatcher.defaultRouteName;
if (overridePlatformDefaultLocation) {
opxdelwin marked this conversation as resolved.
Show resolved Hide resolved
// can force null check as it's already verified by
// asset() while initialization
return initialLocation!;
}
if (initialLocation == null) {
return platformDefault;
} else if (platformDefault == '/') {
Expand Down
3 changes: 2 additions & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
version: 10.2.0
version: 10.2.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
48 changes: 48 additions & 0 deletions packages/go_router/test/go_router_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4909,6 +4909,54 @@ void main() {
expect(statefulWidgetKeyB.currentState?.counter, equals(1));
});
});

///Regression tests for https://github.com/flutter/flutter/issues/132557
group('overridePlatformDefaultLocation', () {
test('No initial location provided', () {
opxdelwin marked this conversation as resolved.
Show resolved Hide resolved
expect(
() => GoRouter(
overridePlatformDefaultLocation: true,
routes: <RouteBase>[
GoRoute(
path: '/a',
builder: (BuildContext context, GoRouterState state) =>
const Placeholder(),
),
GoRoute(
path: '/b',
builder: (BuildContext context, GoRouterState state) =>
const Placeholder(),
),
],
),
throwsA(const TypeMatcher<AssertionError>()));
});
test('Test override using routeInformationProvider', () {
final String platformRoute =
opxdelwin marked this conversation as resolved.
Show resolved Hide resolved
WidgetsBinding.instance.platformDispatcher.defaultRouteName;
const String expectedInitialRoute = '/abc';
expect(platformRoute != expectedInitialRoute, isTrue);

final GoRouter router = GoRouter(
overridePlatformDefaultLocation: true,
initialLocation: '/abc',
routes: <RouteBase>[
GoRoute(
path: '/abc',
builder: (BuildContext context, GoRouterState state) =>
const Placeholder(),
),
GoRoute(
path: '/bcd',
builder: (BuildContext context, GoRouterState state) =>
const Placeholder(),
),
],
);
expect(router.routeInformationProvider.value.uri.toString(),
expectedInitialRoute);
});
});
}

class TestInheritedNotifier extends InheritedNotifier<ValueNotifier<String>> {
Expand Down