diff --git a/AUTHORS b/AUTHORS index 2d6cec4ff91..6ea7eeec4f5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -76,3 +76,4 @@ Daniele Cambi Michele Benedetti Taskulu LDA LinXunFeng +Hashir Shoaib diff --git a/packages/go_router/AUTHORS b/packages/go_router/AUTHORS index fa2a9bee3ba..7174a736ef5 100644 --- a/packages/go_router/AUTHORS +++ b/packages/go_router/AUTHORS @@ -5,3 +5,4 @@ Google Inc. csells@sellsbrothers.com +Hashir Shoaib diff --git a/packages/go_router/CHANGELOG.md b/packages/go_router/CHANGELOG.md index d33dd28b237..7b3e451ba90 100644 --- a/packages/go_router/CHANGELOG.md +++ b/packages/go_router/CHANGELOG.md @@ -1,3 +1,7 @@ +## 14.1.3 + +- Improves the logging of routes when `debugLogDiagnostics` is enabled or `debugKnownRoutes() is called. Explains the position of shell routes in the route tree. Prints the widget name of the routes it is building. + ## 14.1.2 - Fixes issue that path parameters are not set when using the `goBranch`. diff --git a/packages/go_router/lib/src/configuration.dart b/packages/go_router/lib/src/configuration.dart index bef882e1460..d1cca5ce61f 100644 --- a/packages/go_router/lib/src/configuration.dart +++ b/packages/go_router/lib/src/configuration.dart @@ -525,7 +525,8 @@ class RouteConfiguration { String debugKnownRoutes() { final StringBuffer sb = StringBuffer(); sb.writeln('Full paths for routes:'); - _debugFullPathsFor(_routingConfig.value.routes, '', 0, sb); + _debugFullPathsFor( + _routingConfig.value.routes, '', const <_DecorationType>[], sb); if (_nameToPath.isNotEmpty) { sb.writeln('known full paths for route names:'); @@ -538,15 +539,50 @@ class RouteConfiguration { } void _debugFullPathsFor(List routes, String parentFullpath, - int depth, StringBuffer sb) { - for (final RouteBase route in routes) { + List<_DecorationType> parentDecoration, StringBuffer sb) { + for (final (int index, RouteBase route) in routes.indexed) { + final List<_DecorationType> decoration = + _getDecoration(parentDecoration, index, routes.length); + final String decorationString = + decoration.map((_DecorationType e) => e.toString()).join(); + String path = parentFullpath; if (route is GoRoute) { - final String fullPath = concatenatePaths(parentFullpath, route.path); - sb.writeln(' => ${''.padLeft(depth * 2)}$fullPath'); - _debugFullPathsFor(route.routes, fullPath, depth + 1, sb); + path = concatenatePaths(parentFullpath, route.path); + final String? screenName = + route.builder?.runtimeType.toString().split('=> ').last; + sb.writeln('$decorationString$path ' + '${screenName == null ? '' : '($screenName)'}'); } else if (route is ShellRouteBase) { - _debugFullPathsFor(route.routes, parentFullpath, depth, sb); + sb.writeln('$decorationString (ShellRoute)'); + } + _debugFullPathsFor(route.routes, path, decoration, sb); + } + } + + List<_DecorationType> _getDecoration( + List<_DecorationType> parentDecoration, + int index, + int length, + ) { + final Iterable<_DecorationType> newDecoration = + parentDecoration.map((_DecorationType e) { + switch (e) { + // swap + case _DecorationType.branch: + return _DecorationType.parentBranch; + case _DecorationType.leaf: + return _DecorationType.none; + // no swap + case _DecorationType.parentBranch: + return _DecorationType.parentBranch; + case _DecorationType.none: + return _DecorationType.none; } + }); + if (index == length - 1) { + return <_DecorationType>[...newDecoration, _DecorationType.leaf]; + } else { + return <_DecorationType>[...newDecoration, _DecorationType.branch]; } } @@ -575,3 +611,18 @@ class RouteConfiguration { } } } + +enum _DecorationType { + parentBranch('│ '), + branch('├─'), + leaf('└─'), + none(' '), + ; + + const _DecorationType(this.value); + + final String value; + + @override + String toString() => value; +} diff --git a/packages/go_router/pubspec.yaml b/packages/go_router/pubspec.yaml index 0694585b544..578b394be77 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: 14.1.2 +version: 14.1.3 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/test/configuration_test.dart b/packages/go_router/test/configuration_test.dart index a35f2b87e81..0929756599f 100644 --- a/packages/go_router/test/configuration_test.dart +++ b/packages/go_router/test/configuration_test.dart @@ -1070,15 +1070,17 @@ void main() { }, ).debugKnownRoutes(), 'Full paths for routes:\n' - ' => /a\n' - ' => /a/b\n' - ' => /a/c\n' - ' => /d\n' - ' => /d/e\n' - ' => /d/e/f\n' - ' => /g\n' - ' => /g/h\n' - ' => /g/i\n', + '├─/a (Widget)\n' + '│ └─ (ShellRoute)\n' + '│ ├─/a/b (Widget)\n' + '│ └─/a/c (Widget)\n' + '├─/d (Widget)\n' + '│ └─/d/e (Widget)\n' + '│ └─/d/e/f (Widget)\n' + '└─/g (Widget)\n' + ' └─ (ShellRoute)\n' + ' ├─/g/h (Widget)\n' + ' └─/g/i (Widget)\n', ); }, ); diff --git a/packages/go_router/test/logging_test.dart b/packages/go_router/test/logging_test.dart index 1303f2909db..d3251ee1b14 100644 --- a/packages/go_router/test/logging_test.dart +++ b/packages/go_router/test/logging_test.dart @@ -75,7 +75,7 @@ void main() { expect( logs, const [ - 'Full paths for routes:\n => /\n', + 'Full paths for routes:\n└─/ (Text)\n', 'setting initial location null' ], reason: 'Go router should have sent the 2 events to the logger', @@ -110,7 +110,7 @@ void main() { expect( logs, const [ - 'Full paths for routes:\n => /\n', + 'Full paths for routes:\n└─/ (Text)\n', 'setting initial location null' ], reason: 'Go router should have sent the 2 events to the logger',