[go_router_builder] add support to go_router_builder for initializing a StatefulShellBranch with observers (#5865)
This pr added the ability to provide a list of NavigatorObservers to a StatefulShellBranch.
The equivalent was never added to go_router_builder, so this pr provides that functionality.
With this change, you can provide NavigatorObservers directly to the StatefulShellBranch and fix https://github.com/flutter/flutter/issues/143869.
diff --git a/packages/go_router_builder/CHANGELOG.md b/packages/go_router_builder/CHANGELOG.md
index 096e927..e4ea9f6 100644
--- a/packages/go_router_builder/CHANGELOG.md
+++ b/packages/go_router_builder/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 2.6.0
+
+* Adds support for passing observers to the StatefulShellBranch for the nested Navigator.
+
## 2.5.1
- Updates examples to use uri.path instead of uri.toString() for accessing the current location.
diff --git a/packages/go_router_builder/lib/src/route_config.dart b/packages/go_router_builder/lib/src/route_config.dart
index 599ff12..0cfa7a3 100644
--- a/packages/go_router_builder/lib/src/route_config.dart
+++ b/packages/go_router_builder/lib/src/route_config.dart
@@ -146,6 +146,7 @@
required this.navigatorKey,
required super.routeDataClass,
required super.parent,
+ required this.observers,
this.restorationScopeId,
this.initialLocation,
}) : super._();
@@ -159,6 +160,9 @@
/// The initial route.
final String? initialLocation;
+ /// The navigator observers.
+ final String? observers;
+
@override
Iterable<String> classDeclarations() => <String>[];
@@ -168,7 +172,8 @@
String get routeConstructorParameters =>
'${navigatorKey == null ? '' : 'navigatorKey: $navigatorKey,'}'
'${restorationScopeId == null ? '' : 'restorationScopeId: $restorationScopeId,'}'
- '${initialLocation == null ? '' : 'initialLocation: $initialLocation,'}';
+ '${initialLocation == null ? '' : 'initialLocation: $initialLocation,'}'
+ '${observers == null ? '' : 'observers: $observers,'}';
@override
String get routeDataClassName => 'StatefulShellBranchData';
@@ -521,6 +526,10 @@
classElement,
parameterName: r'$initialLocation',
),
+ observers: _generateParameterGetterCode(
+ classElement,
+ parameterName: r'$observers',
+ ),
);
case 'TypedGoRoute':
final ConstantReader pathValue = reader.read('path');
diff --git a/packages/go_router_builder/pubspec.yaml b/packages/go_router_builder/pubspec.yaml
index b552204..c5a6563 100644
--- a/packages/go_router_builder/pubspec.yaml
+++ b/packages/go_router_builder/pubspec.yaml
@@ -2,12 +2,13 @@
description: >-
A builder that supports generated strongly-typed route helpers for
package:go_router
-version: 2.5.1
+version: 2.6.0
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
environment:
sdk: ^3.1.0
+ flutter: ">=3.13.0"
dependencies:
analyzer: ">=5.2.0 <7.0.0"
@@ -23,6 +24,8 @@
dev_dependencies:
build_test: ^2.1.7
dart_style: 2.3.2
+ flutter:
+ sdk: flutter
go_router: ^10.0.0
test: ^1.20.0
diff --git a/packages/go_router_builder/test_inputs/statefull_shell_branch_with_observers_data.dart b/packages/go_router_builder/test_inputs/statefull_shell_branch_with_observers_data.dart
new file mode 100644
index 0000000..07fac00
--- /dev/null
+++ b/packages/go_router_builder/test_inputs/statefull_shell_branch_with_observers_data.dart
@@ -0,0 +1,14 @@
+// Copyright 2013 The Flutter Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+import 'package:flutter/widgets.dart';
+import 'package:go_router/go_router.dart';
+
+@TypedStatefulShellBranch<StatefulShellBranchWithObserversData>()
+class StatefulShellBranchWithObserversData extends StatefulShellBranchData {
+ const StatefulShellBranchWithObserversData();
+
+ static const String $initialLocation = '/main/second-tab';
+ static const List<NavigatorObserver> $observers = <NavigatorObserver>[];
+}
diff --git a/packages/go_router_builder/test_inputs/statefull_shell_branch_with_observers_data.dart.expect b/packages/go_router_builder/test_inputs/statefull_shell_branch_with_observers_data.dart.expect
new file mode 100644
index 0000000..d851a3f
--- /dev/null
+++ b/packages/go_router_builder/test_inputs/statefull_shell_branch_with_observers_data.dart.expect
@@ -0,0 +1,5 @@
+RouteBase get $statefulShellBranchWithObserversData =>
+ StatefulShellBranchData.$branch(
+ initialLocation: StatefulShellBranchWithObserversData.$initialLocation,
+ observers: StatefulShellBranchWithObserversData.$observers,
+ );