[go_router] Feature Request: Expose observers parameter for StatefulShellRoute to support RouteAware in nested navigators #168074
Labels
c: new feature
Nothing broken; request for a new capability
c: proposal
A detailed proposal for a change to Flutter
p: go_router
The go_router package
package
flutter/packages repository. See also p: labels.
team-go_router
Owned by Go Router team
StatefulShellRoute provides a way to implement tabbed navigation with preserved state for each branch (shell). However, I can't find a way to attach NavigatorObserver instances (like RouteObserver) specifically to the nested Navigator widget(s) it manages internally for its branches.
This is a problem when trying to use standard Flutter patterns like the RouteAware mixin on pages within a StatefulShellRoute branch. RouteAware relies on ModalRoute.of(context) to find its associated route and navigator, and then subscribes to a RouteObserver associated with that navigator.
If a RouteObserver is only added to the top-level GoRouter constructor's observers list, it only observes the root navigator. Navigation events happening within the StatefulShellRoute's nested navigator (e.g., pushing a detail page onto a branch's initial page) are not seen by the global observer. Consequently, RouteAware methods (didPush, didPop, didPopNext, didPushNext) do not get called for pages managed by the StatefulShellRoute's navigator.
Example Scenario:
Proposed Solution:
Add an observers parameter to the StatefulShellRoute definition (and consequently to the TypedStatefulShellRoute annotation helper). This parameter would accept a List.
These observers would then be attached to the nested Navigator widget(s) created and managed by the StatefulShellRoute for its branches.
The text was updated successfully, but these errors were encountered: