1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
| class NavigatorState extends State<Navigator> with TickerProviderStateMixin {
final GlobalKey<OverlayState> _overlayKey = GlobalKey<OverlayState>(); final List<Route<dynamic>> _history = <Route<dynamic>>[]; final Set<Route<dynamic>> _poppedRoutes = <Route<dynamic>>{};
final FocusScopeNode focusScopeNode = FocusScopeNode(debugLabel: 'Navigator Scope');
final List<OverlayEntry> _initialOverlayEntries = <OverlayEntry>[];
@override void initState() { super.initState(); for (NavigatorObserver observer in widget.observers) { observer._navigator = this; } String initialRouteName = widget.initialRoute ?? Navigator.defaultRouteName; if (initialRouteName.startsWith('/') && initialRouteName.length > 1) { initialRouteName = initialRouteName.substring(1); assert(Navigator.defaultRouteName == '/'); final List<String> plannedInitialRouteNames = <String>[ Navigator.defaultRouteName, ]; final List<Route<dynamic>> plannedInitialRoutes = <Route<dynamic>>[ _routeNamed<dynamic>(Navigator.defaultRouteName, allowNull: true, arguments: null), ]; final List<String> routeParts = initialRouteName.split('/'); if (initialRouteName.isNotEmpty) { String routeName = ''; for (String part in routeParts) { routeName += '/$part'; plannedInitialRouteNames.add(routeName); plannedInitialRoutes.add(_routeNamed<dynamic>(routeName, allowNull: true, arguments: null)); } } if (plannedInitialRoutes.contains(null)) { push(_routeNamed<Object>(Navigator.defaultRouteName, arguments: null)); } else { plannedInitialRoutes.forEach(push); } } else { Route<Object> route; if (initialRouteName != Navigator.defaultRouteName) route = _routeNamed<Object>(initialRouteName, allowNull: true, arguments: null); route ??= _routeNamed<Object>(Navigator.defaultRouteName, arguments: null); push(route); } for (Route<dynamic> route in _history) _initialOverlayEntries.addAll(route.overlayEntries); }
@override void didUpdateWidget(Navigator oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.observers != widget.observers) { for (NavigatorObserver observer in oldWidget.observers) observer._navigator = null; for (NavigatorObserver observer in widget.observers) { observer._navigator = this; } } for (Route<dynamic> route in _history) route.changedExternalState(); }
@override void dispose() { for (NavigatorObserver observer in widget.observers) observer._navigator = null; final List<Route<dynamic>> doomed = _poppedRoutes.toList()..addAll(_history); for (Route<dynamic> route in doomed) route.dispose(); _poppedRoutes.clear(); _history.clear(); focusScopeNode.dispose(); super.dispose(); }
OverlayState get overlay => _overlayKey.currentState;
OverlayEntry get _currentOverlayEntry { for (Route<dynamic> route in _history.reversed) { if (route.overlayEntries.isNotEmpty) return route.overlayEntries.last; } return null; }
bool _debugLocked = false;
Route<T> _routeNamed<T>(String name, { @required Object arguments, bool allowNull = false }) { final RouteSettings settings = RouteSettings( name: name, isInitialRoute: _history.isEmpty, arguments: arguments, ); Route<T> route = widget.onGenerateRoute(settings); if (route == null && !allowNull) { route = widget.onUnknownRoute(settings); } return route; }
@optionalTypeArgs Future<T> pushNamed<T extends Object>( String routeName, { Object arguments, }) { return push<T>(_routeNamed<T>(routeName, arguments: arguments)); }
@optionalTypeArgs Future<T> pushReplacementNamed<T extends Object, TO extends Object>( String routeName, { TO result, Object arguments, }) { return pushReplacement<T, TO>(_routeNamed<T>(routeName, arguments: arguments), result: result); }
@optionalTypeArgs Future<T> popAndPushNamed<T extends Object, TO extends Object>( String routeName, { TO result, Object arguments, }) { pop<TO>(result); return pushNamed<T>(routeName, arguments: arguments); }
@optionalTypeArgs Future<T> pushNamedAndRemoveUntil<T extends Object>( String newRouteName, RoutePredicate predicate, { Object arguments, }) { return pushAndRemoveUntil<T>(_routeNamed<T>(newRouteName, arguments: arguments), predicate); }
@optionalTypeArgs Future<T> push<T extends Object>(Route<T> route) { final Route<dynamic> oldRoute = _history.isNotEmpty ? _history.last : null; route._navigator = this; route.install(_currentOverlayEntry); _history.add(route); route.didPush(); route.didChangeNext(null); if (oldRoute != null) { oldRoute.didChangeNext(route); route.didChangePrevious(oldRoute); } for (NavigatorObserver observer in widget.observers) observer.didPush(route, oldRoute); RouteNotificationMessages.maybeNotifyRouteChange(_routePushedMethod, route, oldRoute); _afterNavigation(route); return route.popped; }
void _afterNavigation<T>(Route<T> route) { _cancelActivePointers(); }
@optionalTypeArgs Future<T> pushReplacement<T extends Object, TO extends Object>(Route<T> newRoute, { TO result }) { final Route<dynamic> oldRoute = _history.last; final int index = _history.length - 1; newRoute._navigator = this; newRoute.install(_currentOverlayEntry); _history[index] = newRoute; newRoute.didPush().whenCompleteOrCancel(() { if (mounted) { oldRoute ..didComplete(result ?? oldRoute.currentResult) ..dispose(); } }); newRoute.didChangeNext(null); if (index > 0) { _history[index - 1].didChangeNext(newRoute); newRoute.didChangePrevious(_history[index - 1]); } for (NavigatorObserver observer in widget.observers) observer.didReplace(newRoute: newRoute, oldRoute: oldRoute); RouteNotificationMessages.maybeNotifyRouteChange(_routeReplacedMethod, newRoute, oldRoute); _afterNavigation(newRoute); return newRoute.popped; }
@optionalTypeArgs Future<T> pushAndRemoveUntil<T extends Object>(Route<T> newRoute, RoutePredicate predicate) { final Route<dynamic> precedingRoute = _history.isNotEmpty ? _history.last : null; final OverlayEntry precedingRouteOverlay = _currentOverlayEntry;
final List<Route<dynamic>> removedRoutes = <Route<dynamic>>[]; while (_history.isNotEmpty && !predicate(_history.last)) { final Route<dynamic> removedRoute = _history.removeLast(); removedRoutes.add(removedRoute); }
final Route<dynamic> newPrecedingRoute = _history.isNotEmpty ? _history.last : null; newRoute._navigator = this; newRoute.install(precedingRouteOverlay); _history.add(newRoute);
newRoute.didPush().whenCompleteOrCancel(() { if (mounted) { for (Route<dynamic> removedRoute in removedRoutes) { for (NavigatorObserver observer in widget.observers) observer.didRemove(removedRoute, newPrecedingRoute); removedRoute.dispose(); }
if (newPrecedingRoute != null) newPrecedingRoute.didChangeNext(newRoute); } });
newRoute.didChangeNext(null); for (NavigatorObserver observer in widget.observers) observer.didPush(newRoute, precedingRoute); _afterNavigation(newRoute); return newRoute.popped; }
@optionalTypeArgs void replace<T extends Object>({ @required Route<dynamic> oldRoute, @required Route<T> newRoute }) { if (oldRoute == newRoute) return; final int index = _history.indexOf(oldRoute); newRoute._navigator = this; newRoute.install(oldRoute.overlayEntries.last); _history[index] = newRoute; newRoute.didReplace(oldRoute); if (index + 1 < _history.length) { newRoute.didChangeNext(_history[index + 1]); _history[index + 1].didChangePrevious(newRoute); } else { newRoute.didChangeNext(null); } if (index > 0) { _history[index - 1].didChangeNext(newRoute); newRoute.didChangePrevious(_history[index - 1]); } for (NavigatorObserver observer in widget.observers) observer.didReplace(newRoute: newRoute, oldRoute: oldRoute); RouteNotificationMessages.maybeNotifyRouteChange(_routeReplacedMethod, newRoute, oldRoute); oldRoute.dispose(); }
@optionalTypeArgs void replaceRouteBelow<T extends Object>({ @required Route<dynamic> anchorRoute, Route<T> newRoute }) { replace<T>(oldRoute: _history[_history.indexOf(anchorRoute) - 1], newRoute: newRoute); }
bool canPop() { return _history.length > 1 || _history[0].willHandlePopInternally; }
@optionalTypeArgs Future<bool> maybePop<T extends Object>([ T result ]) async { final Route<T> route = _history.last; final RoutePopDisposition disposition = await route.willPop(); if (disposition != RoutePopDisposition.bubble && mounted) { if (disposition == RoutePopDisposition.pop) pop(result); return true; } return false; }
@optionalTypeArgs bool pop<T extends Object>([ T result ]) { final Route<dynamic> route = _history.last; bool debugPredictedWouldPop; if (route.didPop(result ?? route.currentResult)) { if (_history.length > 1) { _history.removeLast(); if (route._navigator != null) _poppedRoutes.add(route); _history.last.didPopNext(route); for (NavigatorObserver observer in widget.observers) observer.didPop(route, _history.last); RouteNotificationMessages.maybeNotifyRouteChange(_routePoppedMethod, route, _history.last); } else { return false; } } _afterNavigation<dynamic>(route); return true; }
void popUntil(RoutePredicate predicate) { while (!predicate(_history.last)) pop(); }
void removeRoute(Route<dynamic> route) { final int index = _history.indexOf(route); final Route<dynamic> previousRoute = index > 0 ? _history[index - 1] : null; final Route<dynamic> nextRoute = (index + 1 < _history.length) ? _history[index + 1] : null; _history.removeAt(index); previousRoute?.didChangeNext(nextRoute); nextRoute?.didChangePrevious(previousRoute); for (NavigatorObserver observer in widget.observers) observer.didRemove(route, previousRoute); route.dispose(); _afterNavigation<dynamic>(nextRoute); }
void removeRouteBelow(Route<dynamic> anchorRoute) { final int index = _history.indexOf(anchorRoute) - 1; final Route<dynamic> targetRoute = _history[index]; _history.removeAt(index); final Route<dynamic> nextRoute = index < _history.length ? _history[index] : null; final Route<dynamic> previousRoute = index > 0 ? _history[index - 1] : null; if (previousRoute != null) previousRoute.didChangeNext(nextRoute); if (nextRoute != null) nextRoute.didChangePrevious(previousRoute); targetRoute.dispose(); }
void finalizeRoute(Route<dynamic> route) { _poppedRoutes.remove(route); route.dispose(); }
bool get userGestureInProgress => _userGesturesInProgress > 0; int _userGesturesInProgress = 0;
void didStartUserGesture() { _userGesturesInProgress += 1; if (_userGesturesInProgress == 1) { final Route<dynamic> route = _history.last; final Route<dynamic> previousRoute = !route.willHandlePopInternally && _history.length > 1 ? _history[_history.length - 2] : null; for (NavigatorObserver observer in widget.observers) observer.didStartUserGesture(route, previousRoute); } }
void didStopUserGesture() { _userGesturesInProgress -= 1; if (_userGesturesInProgress == 0) { for (NavigatorObserver observer in widget.observers) observer.didStopUserGesture(); } } final Set<int> _activePointers = <int>{};
void _handlePointerDown(PointerDownEvent event) { _activePointers.add(event.pointer); }
void _handlePointerUpOrCancel(PointerEvent event) { _activePointers.remove(event.pointer); }
void _cancelActivePointers() { if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.idle) { final RenderAbsorbPointer absorber = _overlayKey.currentContext?.ancestorRenderObjectOfType(const TypeMatcher<RenderAbsorbPointer>()); setState(() { absorber?.absorbing = true; }); } _activePointers.toList().forEach(WidgetsBinding.instance.cancelPointer); }
@override Widget build(BuildContext context) { return Listener( onPointerDown: _handlePointerDown, onPointerUp: _handlePointerUpOrCancel, onPointerCancel: _handlePointerUpOrCancel, child: AbsorbPointer( absorbing: false, child: FocusScope( node: focusScopeNode, autofocus: true, child: Overlay( key: _overlayKey, initialEntries: _initialOverlayEntries, ), ), ), ); } }
|