Junior — Middle
Do you have practical experience using Navigator 2.0 for navigation management in Flutter applications?
sobes.tech AI
Answer from AI
Yes, I have practical experience using Navigator 2.0 in Flutter. This approach allows for more flexible navigation management, especially in complex applications with dynamic routes and deep links.
Navigator 2.0 is based on declarative navigation state management through Router and RouteInformationParser, which simplifies URL and app state synchronization.
An example of simple usage of Navigator 2.0:
class MyRouterDelegate extends RouterDelegate<MyRoutePath>
with ChangeNotifier, PopNavigatorRouterDelegateMixin<MyRoutePath> {
final GlobalKey<NavigatorState> navigatorKey;
bool showDetails = false;
MyRouterDelegate() : navigatorKey = GlobalKey<NavigatorState>();
@override
MyRoutePath get currentConfiguration =>
showDetails ? MyRoutePath.details() : MyRoutePath.home();
@override
Widget build(BuildContext context) {
return Navigator(
key: navigatorKey,
pages: [
MaterialPage(child: HomeScreen(onTapped: () {
showDetails = true;
notifyListeners();
})),
if (showDetails) MaterialPage(child: DetailsScreen())
],
onPopPage: (route, result) {
if (!route.didPop()) return false;
showDetails = false;
notifyListeners();
return true;
},
);
}
@override
Future<void> setNewRoutePath(MyRoutePath path) async {
showDetails = path.isDetailsPage;
}
}
This approach makes it easy to manage the page stack and maintain navigation considering the application's state.