From 50383a26574122396c39f5d4502cf20b20c54781 Mon Sep 17 00:00:00 2001 From: Ahmed Elsayed Date: Sat, 6 Apr 2024 04:44:00 +0200 Subject: [PATCH 1/6] Access current location using uri.path to support deep links --- .../go_router/example/lib/shell_route.dart | 2 +- .../example/lib/shell_route_top_route.dart | 2 +- .../go_router/test/go_router_state_test.dart | 16 +++++------- .../example/lib/all_types.dart | 26 +++++++++---------- .../example/lib/shell_route_example.dart | 2 +- .../lib/shell_route_with_keys_example.dart | 2 +- .../shell_route_with_observers_example.dart | 2 +- 7 files changed, 25 insertions(+), 27 deletions(-) diff --git a/packages/go_router/example/lib/shell_route.dart b/packages/go_router/example/lib/shell_route.dart index 2d5d87657094..1628151d9371 100644 --- a/packages/go_router/example/lib/shell_route.dart +++ b/packages/go_router/example/lib/shell_route.dart @@ -152,7 +152,7 @@ class ScaffoldWithNavBar extends StatelessWidget { } static int _calculateSelectedIndex(BuildContext context) { - final String location = GoRouterState.of(context).uri.toString(); + final String location = GoRouterState.of(context).uri.path; if (location.startsWith('/a')) { return 0; } diff --git a/packages/go_router/example/lib/shell_route_top_route.dart b/packages/go_router/example/lib/shell_route_top_route.dart index 35021297dfca..ce6128a559d8 100644 --- a/packages/go_router/example/lib/shell_route_top_route.dart +++ b/packages/go_router/example/lib/shell_route_top_route.dart @@ -195,7 +195,7 @@ class ScaffoldWithNavBar extends StatelessWidget { } static int _calculateSelectedIndex(BuildContext context) { - final String location = GoRouterState.of(context).uri.toString(); + final String location = GoRouterState.of(context).uri.path; if (location.startsWith('/a')) { return 0; } diff --git a/packages/go_router/test/go_router_state_test.dart b/packages/go_router/test/go_router_state_test.dart index 7a1b73dd93e6..8cb2e4a74178 100644 --- a/packages/go_router/test/go_router_state_test.dart +++ b/packages/go_router/test/go_router_state_test.dart @@ -42,7 +42,7 @@ void main() { path: '/', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text('1 ${GoRouterState.of(context).uri}'); + return Text('1 ${GoRouterState.of(context).uri.path}'); }); }, routes: [ @@ -50,7 +50,7 @@ void main() { path: 'a', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text('2 ${GoRouterState.of(context).uri}'); + return Text('2 ${GoRouterState.of(context).uri.path}'); }); }), ]), @@ -74,7 +74,7 @@ void main() { path: '/', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text('1 ${GoRouterState.of(context).uri}'); + return Text('1 ${GoRouterState.of(context).uri.path}'); }); }, routes: [ @@ -110,7 +110,7 @@ void main() { path: '/', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text(GoRouterState.of(context).uri.toString()); + return Text(GoRouterState.of(context).uri.path); }); }, routes: [ @@ -118,8 +118,7 @@ void main() { path: 'a', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text( - key: key, GoRouterState.of(context).uri.toString()); + return Text(key: key, GoRouterState.of(context).uri.path); }); }), ]), @@ -153,7 +152,7 @@ void main() { path: '/', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text(GoRouterState.of(context).uri.toString()); + return Text(GoRouterState.of(context).uri.path); }); }, routes: [ @@ -161,8 +160,7 @@ void main() { path: 'a', builder: (_, __) { return Builder(builder: (BuildContext context) { - return Text( - key: key, GoRouterState.of(context).uri.toString()); + return Text(key: key, GoRouterState.of(context).uri.path); }); }), ]), diff --git a/packages/go_router_builder/example/lib/all_types.dart b/packages/go_router_builder/example/lib/all_types.dart index aa49e45c83e4..a00c68e78c25 100644 --- a/packages/go_router_builder/example/lib/all_types.dart +++ b/packages/go_router_builder/example/lib/all_types.dart @@ -58,7 +58,7 @@ class BigIntRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('BigIntRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -84,7 +84,7 @@ class BoolRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('BoolRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -107,7 +107,7 @@ class DateTimeRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('DateTimeRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -133,7 +133,7 @@ class DoubleRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('DoubleRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -159,7 +159,7 @@ class IntRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('IntRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -185,7 +185,7 @@ class NumRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('NumRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -212,7 +212,7 @@ class EnumRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('EnumRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -239,7 +239,7 @@ class EnhancedEnumRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('EnhancedEnumRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -265,7 +265,7 @@ class StringRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('StringRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -288,7 +288,7 @@ class UriRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('UriRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -361,7 +361,7 @@ class IterableRoute extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('IterableRoute'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -430,7 +430,7 @@ class IterableRouteWithDefaultValues extends GoRouteData { Widget drawerTile(BuildContext context) => ListTile( title: const Text('IterableRouteWithDefaultValues'), onTap: () => go(context), - selected: GoRouterState.of(context).uri.toString() == location, + selected: GoRouterState.of(context).uri.path == location, ); } @@ -536,7 +536,7 @@ class BasePage extends StatelessWidget { Text( 'Query param with default value: $queryParamWithDefaultValue', ), - SelectableText(GoRouterState.of(context).uri.toString()), + SelectableText(GoRouterState.of(context).uri.path), ], ), ), diff --git a/packages/go_router_builder/example/lib/shell_route_example.dart b/packages/go_router_builder/example/lib/shell_route_example.dart index 6b5ec27947de..93e1df207a88 100644 --- a/packages/go_router_builder/example/lib/shell_route_example.dart +++ b/packages/go_router_builder/example/lib/shell_route_example.dart @@ -77,7 +77,7 @@ class MyShellRouteScreen extends StatelessWidget { final Widget child; int getCurrentIndex(BuildContext context) { - final String location = GoRouterState.of(context).uri.toString(); + final String location = GoRouterState.of(context).uri.path; if (location == '/bar') { return 1; } diff --git a/packages/go_router_builder/example/lib/shell_route_with_keys_example.dart b/packages/go_router_builder/example/lib/shell_route_with_keys_example.dart index af79f41fb989..5448f7e19183 100644 --- a/packages/go_router_builder/example/lib/shell_route_with_keys_example.dart +++ b/packages/go_router_builder/example/lib/shell_route_with_keys_example.dart @@ -57,7 +57,7 @@ class MyShellRouteScreen extends StatelessWidget { final Widget child; int getCurrentIndex(BuildContext context) { - final String location = GoRouterState.of(context).uri.toString(); + final String location = GoRouterState.of(context).uri.path; if (location.startsWith('/users')) { return 1; } diff --git a/packages/go_router_builder/example/lib/shell_route_with_observers_example.dart b/packages/go_router_builder/example/lib/shell_route_with_observers_example.dart index a762e12c1241..a69e05e749ef 100644 --- a/packages/go_router_builder/example/lib/shell_route_with_observers_example.dart +++ b/packages/go_router_builder/example/lib/shell_route_with_observers_example.dart @@ -60,7 +60,7 @@ class MyShellRouteScreen extends StatelessWidget { final Widget child; int getCurrentIndex(BuildContext context) { - final String location = GoRouterState.of(context).uri.toString(); + final String location = GoRouterState.of(context).uri.path; if (location.startsWith('/users')) { return 1; } From f217ae7a3028afc05134615aa23fa9d149ff2687 Mon Sep 17 00:00:00 2001 From: Ahmed Elsayed Date: Mon, 8 Apr 2024 19:39:42 +0200 Subject: [PATCH 2/6] Update version --- packages/go_router/pubspec.yaml | 2 +- packages/go_router_builder/pubspec.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 1fc99858822f..29dfaa27a447 100644 --- a/packages/go_router/pubspec.yaml +++ b/packages/go_router/pubspec.yaml @@ -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: 13.2.3 +version: 13.2.4 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 diff --git a/packages/go_router_builder/pubspec.yaml b/packages/go_router_builder/pubspec.yaml index a3ffb5179650..b552204767ad 100644 --- a/packages/go_router_builder/pubspec.yaml +++ b/packages/go_router_builder/pubspec.yaml @@ -2,7 +2,7 @@ name: go_router_builder description: >- A builder that supports generated strongly-typed route helpers for package:go_router -version: 2.5.0 +version: 2.5.1 repository: https://github.com/flutter/packages/tree/main/packages/go_router_builder issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router_builder%22 From 827b7ad04a8353175db4e2adbe58a15bb71fc5b3 Mon Sep 17 00:00:00 2001 From: Ahmed Elsayed Date: Mon, 8 Apr 2024 19:41:01 +0200 Subject: [PATCH 3/6] Update Changelog --- packages/go_router/CHANGELOG.md | 4 ++++ packages/go_router_builder/CHANGELOG.md | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index 774d71c756e8..576a71789a7c 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 13.2.4 + +- Updates examples to use uri.path instead of uri.toString() for accessing the current location. + ## 13.2.3 - Fixes an issue where deep links without path caused an exception diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md index 8c7bf3ee834f..096e927331c8 100644 --- a/packages/go_router_builder/CHANGELOG.md +++ b/packages/go_router_builder/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.5.1 + +- Updates examples to use uri.path instead of uri.toString() for accessing the current location. + ## 2.5.0 * Updates minimum supported SDK version to Flutter 3.13/Dart 3.1. From ab6f793f81e8bef452c4c36a5610d47f81881efd Mon Sep 17 00:00:00 2001 From: Ahmed Elsayed Date: Wed, 10 Apr 2024 22:14:05 +0200 Subject: [PATCH 4/6] Fix tests to adapt uri.path --- .../go_router/test/go_router_state_test.dart | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/go_router/test/go_router_state_test.dart b/packages/go_router/test/go_router_state_test.dart index 8cb2e4a74178..69364f2aa801 100644 --- a/packages/go_router/test/go_router_state_test.dart +++ b/packages/go_router/test/go_router_state_test.dart @@ -56,9 +56,9 @@ void main() { ]), ]; final GoRouter router = await createRouter(routes, tester); - router.go('/?p=123'); + router.go('/'); await tester.pumpAndSettle(); - expect(find.text('1 /?p=123'), findsOneWidget); + expect(find.text('1 /'), findsOneWidget); router.go('/a'); await tester.pumpAndSettle(); @@ -124,8 +124,8 @@ void main() { ]), ]; final GoRouter router = - await createRouter(routes, tester, initialLocation: '/a?p=123'); - expect(tester.widget(find.byKey(key)).data, '/a?p=123'); + await createRouter(routes, tester, initialLocation: '/a'); + expect(tester.widget(find.byKey(key)).data, '/a'); final GoRouterStateRegistry registry = tester .widget( find.byType(GoRouterStateRegistryScope)) @@ -135,7 +135,7 @@ void main() { await tester.pump(); expect(registry.registry.length, 2); // should retain the same location even if the location has changed. - expect(tester.widget(find.byKey(key)).data, '/a?p=123'); + expect(tester.widget(find.byKey(key)).data, '/a'); // Finish the pop animation. await tester.pumpAndSettle(); @@ -166,8 +166,8 @@ void main() { ]), ]; await createRouter(routes, tester, - initialLocation: '/a?p=123', navigatorKey: nav); - expect(tester.widget(find.byKey(key)).data, '/a?p=123'); + initialLocation: '/a', navigatorKey: nav); + expect(tester.widget(find.byKey(key)).data, '/a'); final GoRouterStateRegistry registry = tester .widget( find.byType(GoRouterStateRegistryScope)) @@ -177,7 +177,7 @@ void main() { await tester.pump(); expect(registry.registry.length, 2); // should retain the same location even if the location has changed. - expect(tester.widget(find.byKey(key)).data, '/a?p=123'); + expect(tester.widget(find.byKey(key)).data, '/a'); // Finish the pop animation. await tester.pumpAndSettle(); From 27f9f2e456b15014fb90797a525e96f878136fd7 Mon Sep 17 00:00:00 2001 From: Ahmed Elsayed Date: Thu, 11 Apr 2024 02:44:25 +0200 Subject: [PATCH 5/6] Fix all types example test --- packages/go_router_builder/example/lib/all_types.dart | 1 + packages/go_router_builder/example/test/all_types_test.dart | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/go_router_builder/example/lib/all_types.dart b/packages/go_router_builder/example/lib/all_types.dart index a00c68e78c25..a675637f4c59 100644 --- a/packages/go_router_builder/example/lib/all_types.dart +++ b/packages/go_router_builder/example/lib/all_types.dart @@ -537,6 +537,7 @@ class BasePage extends StatelessWidget { 'Query param with default value: $queryParamWithDefaultValue', ), SelectableText(GoRouterState.of(context).uri.path), + SelectableText(GoRouterState.of(context).uri.queryParameters.toString()), ], ), ), diff --git a/packages/go_router_builder/example/test/all_types_test.dart b/packages/go_router_builder/example/test/all_types_test.dart index 99a3824cf4ec..eafcdeefda0d 100644 --- a/packages/go_router_builder/example/test/all_types_test.dart +++ b/packages/go_router_builder/example/test/all_types_test.dart @@ -135,9 +135,10 @@ void main() { ).go(scaffoldState.context); await tester.pumpAndSettle(); expect(find.text('IterableRoute'), findsOneWidget); + expect(find.text('/iterable-route'), findsOneWidget); expect( find.text( - '/iterable-route?enum-iterable-field=football&int-list-field=1&int-list-field=2&int-list-field=3&enum-only-in-set-field=burger&enum-only-in-set-field=pizza'), + '{enum-iterable-field: football, int-list-field: 3, enum-only-in-set-field: pizza}'), findsOneWidget); }); From 8d9fb75b69aaa4f14fd4ce2b2379a3862eb76f3f Mon Sep 17 00:00:00 2001 From: Ahmed Elsayed Date: Thu, 11 Apr 2024 02:53:08 +0200 Subject: [PATCH 6/6] Fix formatting --- packages/go_router_builder/example/lib/all_types.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/go_router_builder/example/lib/all_types.dart b/packages/go_router_builder/example/lib/all_types.dart index a675637f4c59..63d73caa4c0c 100644 --- a/packages/go_router_builder/example/lib/all_types.dart +++ b/packages/go_router_builder/example/lib/all_types.dart @@ -537,7 +537,8 @@ class BasePage extends StatelessWidget { 'Query param with default value: $queryParamWithDefaultValue', ), SelectableText(GoRouterState.of(context).uri.path), - SelectableText(GoRouterState.of(context).uri.queryParameters.toString()), + SelectableText( + GoRouterState.of(context).uri.queryParameters.toString()), ], ), ),