Skip to content

[go_router] Preserve the state of routes #99124

@chunhtai

Description

@chunhtai

copied from csells/go_router#134

Currently there is no automatic way to have your routes remember their internal state as you navigate between them. This is often a desired UX feature as it is seen commonly on mobile and desktop platforms. For example, when I change tabs in a desktop app like Steam, it remembers my scroll position and selected sorting features (ephemeral view state).

The canonical way to do this in flutter is to use an Offstage widget, or something that wraps one, like an IndexedStack. The classic "nested navigator" pattern is a variation on this technique, where multiple stateful navigators are kept in memory using a TabBarView or IndexedStack.

I'm not sure if this is even possible with GoRouter, but would like to put this idea out there.

This is the core problem to solve. Given:

routes: [
  GoRoute(path: 'page1', pageBuilder: (_, state) => MaterialPage(key: state.pageKey, child: Page1())),
  GoRoute(path: 'page2', pageBuilder: (_, state) => MaterialPage(key: state.pageKey, child: Page2())),
]

When page1 is the path, the result of () => MaterialPage(key: state.pageKey, child: Page2()) is stashed Offstage somehow. Vice versa when `page2 is rendered. It wouldn't load everything at once, but rather lazy load and "remember" pages it had seen before. Likely there should be an option to opt-out certain views for persistence.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions