Junior — Middle
Can you explain the concept and main features of Navigator 2.0 in Flutter?
sobes.tech AI
Answer from AI
Navigator 2.0 in Flutter is a new navigation system that provides a more flexible and declarative way to manage routes and navigation history compared to the classic Navigator 1.0.
Main features of Navigator 2.0:
- Declarative approach: navigation is described through the application's state, not through push/pop method calls.
- Support for URL and deep linking: allows synchronization of navigation state with the URL, which is important for web applications and complex scenarios.
- Flexible route stack management: you can explicitly control the list of pages that are displayed.
- Integration with Router and RouteInformationParser: for parsing URLs and generating navigation state.
Example usage:
class MyRouterDelegate extends RouterDelegate<MyRoutePath>
with ChangeNotifier, PopNavigatorRouterDelegateMixin<MyRoutePath> {
final GlobalKey<NavigatorState> navigatorKey;
List<Page> pages = [];
@override
Widget build(BuildContext context) {
return Navigator(
key: navigatorKey,
pages: List.of(pages),
onPopPage: (route, result) {
if (!route.didPop(result)) return false;
// update navigation state
notifyListeners();
return true;
},
);
}
// other methods for managing navigation
}
Thus, Navigator 2.0 allows creating more complex and manageable navigation scenarios, especially useful for large applications and web platforms.