Skip to content

Commit 840ebd8

Browse files
committed
Add state restoration topic to documentation
1 parent 092d832 commit 840ebd8

12 files changed

+655
-343
lines changed

packages/go_router/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 16.2.1
2+
3+
- Adds state restoration topic to documentation
4+
15
## 16.2.0
26

37
- Adds `RelativeGoRouteData` and `TypedRelativeGoRoute`.

packages/go_router/dartdoc_options.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ dartdoc:
3333
"Error handling":
3434
markdown: doc/error-handling.md
3535
name: Error handling
36+
"State restoration":
37+
markdown: doc/state-restoration.md
38+
name: State restoration
3639
categoryOrder:
3740
- "Get started"
3841
- "Upgrading"
@@ -45,6 +48,7 @@ dartdoc:
4548
- "Type-safe routes"
4649
- "Named routes"
4750
- "Error handling"
51+
- "State restoration"
4852
showUndocumentedCategories: true
4953
ignore:
5054
- broken-link
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
## What is state restoration?
2+
3+
State restoration refers to the process of persisting and restoring serialized state
4+
after the app has been killed by the operating system in the background.
5+
For more information, see [Restore state on Android](https://docs.flutter.dev/platform-integration/android/restore-state-android)
6+
and [Restore state on iOS](https://docs.flutter.dev/platform-integration/ios/restore-state-ios).
7+
8+
State restoration does not refer to general purpose state persistence.
9+
For keeping multiple navigation branches in memory at the same time,
10+
see [StatefulShellRoute](https://pub.dev/documentation/go_router/latest/go_router/StatefulShellRoute-class.html).
11+
12+
## Support
13+
14+
GoRouter fully supports state restoration.
15+
16+
To enable state restoration, a top-level configuration is needed
17+
as well as additional configuration depending on the types of routes used.
18+
19+
## Top-level configuration
20+
21+
Add `restorationScopeId`s to `GoRouter` and `MaterialApp.router`:
22+
23+
```dart
24+
final _router = GoRouter(
25+
restorationScopeId: 'router',
26+
routes: [
27+
...
28+
],
29+
);
30+
```
31+
32+
```dart
33+
class MyApp extends StatelessWidget {
34+
@override
35+
Widget build(BuildContext context) {
36+
return MaterialApp.router(
37+
restorationScopeId: 'app',
38+
routerConfig: _router,
39+
);
40+
}
41+
}
42+
```
43+
44+
## Route-specific configuration
45+
46+
### GoRoute
47+
48+
For a `GoRoute` that uses `pageBuilder`, supply a `restorationId` to the page:
49+
50+
```dart
51+
GoRoute(
52+
pageBuilder: (context, state) {
53+
return MaterialPage(
54+
restorationId: 'detailsPage',
55+
path: '/details',
56+
child: DetailsPage(),
57+
);
58+
}
59+
)
60+
```
61+
62+
For a `GoRoute` that does not use `pageBuilder`, no additional configuration
63+
is needed.
64+
65+
For a runnable example with tests, see the [GoRoute state restoration example](https://github.com/flutter/packages/tree/main/packages/go_router/example/lib/state_restoration/go_route_state_restoration.dart).
66+
67+
### ShellRoute
68+
69+
Add a unique `restorationScopeId` to the `ShellRoute`.
70+
71+
Additionally, add a `pageBuilder` and supply a `restorationId` to the page.
72+
73+
A `pageBuilder` which returns a page with `restorationId` must be supplied for state restoration to work.
74+
75+
For a runnable example with tests, see the [ShellRoute state restoration example](https://github.com/flutter/packages/tree/main/packages/go_router/example/lib/state_restoration/shell_route_state_restoration.dart).
76+
77+
```dart
78+
ShellRoute(
79+
restorationScopeId: 'onboardingShell',
80+
pageBuilder: (context, state, child) {
81+
return MaterialPage(
82+
restorationId: 'onboardingPage',
83+
child: OnboardingScaffold(child: child),
84+
);
85+
},
86+
routes: [
87+
...
88+
],
89+
)
90+
```
91+
92+
### StatefulShellRoute
93+
94+
Add a `restorationScopeId` to the `StatefulShellRoute` and a
95+
`pageBuilder` which returns a page with a `restorationId`.
96+
97+
Additionally, add a `restorationScopeId` to each `StatefulShellBranch`.
98+
99+
A `pageBuilder` which returns a page with `restorationId` must be supplied for state restoration to work.
100+
101+
For a runnable example with tests, see the [StatefulShellRoute state restoration example](https://github.com/flutter/packages/tree/main/packages/go_router/example/lib/state_restoration/stateful_shell_route_state_restoration.dart).
102+
103+
```dart
104+
StatefulShellRoute.indexedStack(
105+
restorationScopeId: 'appShell',
106+
pageBuilder: (context, state, navigationShell) {
107+
return MaterialPage(
108+
restorationId: 'appShellPage',
109+
child: AppShell(navigationShell: navigationShell),
110+
);
111+
},
112+
branches: [
113+
StatefulShellBranch(
114+
restorationScopeId: 'homeBranch',
115+
routes: [
116+
...
117+
],
118+
),
119+
StatefulShellBranch(
120+
restorationScopeId: 'profileBranch',
121+
routes: [
122+
...
123+
],
124+
),
125+
],
126+
)
127+
```

packages/go_router/example/lib/others/state_restoration.dart

Lines changed: 0 additions & 101 deletions
This file was deleted.

0 commit comments

Comments
 (0)