Flutter Navigator _RouteEntry
The _RouteEntry class in Flutter, typically an internal class indicated by the leading underscore in its name, serves as a crucial component in the Flutter navigation system. It's not part of the public API, but understanding its role can provide deeper insights into how navigation works in Flutter.
Core Functionality and Role
-
Representation of Route in Navigator's Stack:
_RouteEntryacts as a container or wrapper for aRoutewithin theNavigator's internal stack. Each_RouteEntrycorresponds to aRoutethat has been pushed onto the Navigator. -
Lifecycle State Tracking: It tracks the lifecycle state of the
Routeit contains. This includes states like whether the route is currently being pushed, popped, or is idle. -
Transition Management:
_RouteEntryplays a key role in managing the transitions and animations as theRouteenters or exits the screen.- It coordinates the animations and ensures they are in sync with the route's current state.
_RouteEntryextends _RouteEntry class.
-
Result Handling: It also handles the result of a route when it's popped. This is important for scenarios where routes return data upon being popped.
-
Overlay Management: Since routes often have associated
Overlayentries (for displaying the route's UI),_RouteEntryhelps manage theseOverlayentries, including their insertion and removal from theOverlay.
Usage
_history field of NavigatorState
Signature:
List<_RouteEntry> _history = <_RouteEntry>[];
Core Functionality:
-
Route Stack Management: The
_historylist inNavigatorStateserves as the stack that holds the routes managed by theNavigator. Each item in this list is an instance of_RouteEntry, which, as previously discussed, represents aRoutealong with its lifecycle state and overlay entries. -
Order and Navigation Logic: The order of the
_RouteEntryobjects in the_historylist defines the navigation stack. The last item in the list represents the topmost route in the Navigator, which is the currently visible route if the app is running. -
Transition and State Tracking: As routes are pushed and popped from the Navigator,
_RouteEntryobjects are added to or removed from the_history. This process is accompanied by the appropriate state transitions and animations for each route, as dictated by their respective_RouteEntryinstances. -
Lifecycle Management:
_historyhelps manage the lifecycle of routes. When a new route is pushed, a new_RouteEntryis created and added to this list. When a route is popped, its corresponding_RouteEntryis removed, and the route undergoes its exit animation and eventual disposal.
Usage in NavigatorState:
-
Navigation Operations: Operations like
push,pop,replace, and others modify the_historylist to reflect the changes in the navigation stack. These operations handle the appropriate animations and transitions for the routes. -
Restoration and State Saving: With
NavigatorStatealso mixing inRestorationMixin, the_historyplays a role in state restoration, helping to restore the navigation stack to its previous state when the app is restarted. -
Overlay Integration: The
_overlayKeyinNavigatorStateis used to manage the overlay entries for the routes. As routes are added to or removed from_history, their associated overlay entries are also managed accordingly.
本文作者:Maeiee
本文链接:Flutter Navigator _RouteEntry
版权声明:如无特别声明,本文即为原创文章,版权归 Maeiee 所有,未经允许不得转载!
喜欢我文章的朋友请随缘打赏,鼓励我创作更多更好的作品!
