Flutter Widget Navigator Route
Subjects
Flutter Navigator Route _RouteLifecycle
SubClasses
- Route
- FakeRoute
- OverlayRoute
- TransitionRoute
- ModalRoute
- PageRoute
- CupertinoRouteTransitionMixin
- CupertinoPageRoute
- MaterialRouteTransitionMixin
- MaterialPageRoute
- PageRouteBuilder
- PopupRoute
- PageRoute
- ModalRoute
- TransitionRoute
- LocalHistoryRoute
- CupertinoModalPopupRoute
- RawDialogRoute
- CupertinoDialogRoute
- DialogRoute
- CupertinoRouteTransitionMixin
- CupertinoPageRoute
- CupertinoModalPopupRoute
- CupertinoDialogRoute
_ModalBottomSheetRoute
didPop
The didPop
method in a Flutter Route
plays a critical role in determining how the route responds to a pop request. The difference between returning true
and false
from this method has significant implications for the route's lifecycle and behavior in the navigator stack:
Returning true
from didPop
:
-
Navigator Removes the Route: When
didPop
returnstrue
, it indicates that the route agrees to be popped (removed) from the navigator stack. -
Route's Responsibility to Finalize: Although the navigator removes the route from the history, it doesn't immediately dispose of the route. The route is responsible for calling
NavigatorState.finalizeRoute
, which then leads to its disposal. This mechanism allows the route to perform an exit animation or other visual effects before being completely disposed of. -
Immediate Resolution of
popped
Future: ThedidPop
method should calldidComplete
to resolve thepopped
future. This resolution should occur regardless of any ongoing exit animations, ensuring that the future is resolved as soon as the route agrees to pop.
Returning false
from didPop
:
-
Route Handles Pop Internally: Returning
false
suggests that the route wants to handle the pop request internally. This might be the case if the route manages its own internal state or navigation stack (like nested navigators or tabs). -
Prevents Default Pop Behavior: By returning
false
, the route prevents the default behavior ofNavigatorState.pop
. This means the navigator does not automatically remove the route from the history. -
Maintains Route in History: Since the navigator does not remove the route from the stack, the route remains active and visible in the navigator's history.
本文作者:Maeiee
本文链接:Flutter Widget Navigator Route
版权声明:如无特别声明,本文即为原创文章,版权归 Maeiee 所有,未经允许不得转载!
喜欢我文章的朋友请随缘打赏,鼓励我创作更多更好的作品!