To incorporate restorable push navigation with the get package in Flutter, you can follow the following steps:
dependencies:
get: ^4.1.4
Navigator
class and overrides the push
and pushReplacement
methods:class RestorableNavigator extends Navigator {
RestorableNavigator({
required GlobalKey<NavigatorState> key,
required List<Route<dynamic>> pages,
required this.restorationId,
RoutePredicate? onPopPage,
}) : super(
key: key,
pages: pages,
onPopPage: onPopPage,
);
final String restorationId;
@override
void didUpdateWidget(Navigator oldWidget) {
super.didUpdateWidget(oldWidget);
this.restorationId = oldWidget.restorationId;
}
@override
Route push(Route route) {
route = MaterialPageRoute(
builder: route.builder,
settings: RouteSettings(
name: route.settings.name,
isInitialRoute: pages.isEmpty,
),
);
return super.push(route);
}
@override
Route pushReplacement(Route route, {Object? result}) {
route = MaterialPageRoute(
builder: route.builder,
settings: RouteSettings(
name: route.settings.name,
isInitialRoute: pages.isEmpty,
),
);
return super.pushReplacement(route, result: result);
}
}
RestorationScope
widget that wraps your app and provides a unique restoration ID:RestorationScope(
restorationId: 'main',
child: MaterialApp(
// ...
),
)
Navigator.restorablePush
and Navigator.restorablePushReplacement
methods provided by the get
package to navigate to new screens:Navigator.restorablePush(
context,
MaterialPageRoute(builder: (context) => MyNewPage()),
)
Navigator.restorablePushReplacement(
context,
MaterialPageRoute(builder: (context) => MyNewPage()),
)
The get
package will automatically handle saving and restoring the navigation history, so your users can pick up where they left off even if they close and reopen your app.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-09-13 11:00:00 +0000
Seen: 9 times
Last updated: Feb 07 '22
How can I highlight the home navigation when I am on a subroute, such as blog/post, in Next.js?
How can I include a right-click action for the navigation menu in Angular?
How to stop the navigation bar from rebuilding during navigation in Flutter's widget structure?
What is the process of personalizing the navigation title view for a MAUI ContentPage?
What is the process for using a module to include a new block in the navigation of Prestashop?