navigator.dart 22.3 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'framework.dart';
Adam Barth's avatar
Adam Barth committed
6
import 'overlay.dart';
7

8 9 10 11 12 13
/// An abstraction for an entry managed by a [Navigator].
///
/// This class defines an abstract interface between the navigator and the
/// "routes" that are pushed on and popped off the navigator. Most routes have
/// visual affordances, which they place in the navigators [Overlay] using one
/// or more [OverlayEntry] objects.
Hixie's avatar
Hixie committed
14
abstract class Route<T> {
15 16 17 18
  /// The navigator that the route is in, if any.
  NavigatorState get navigator => _navigator;
  NavigatorState _navigator;

19
  /// The overlay entries for this route.
20 21 22
  List<OverlayEntry> get overlayEntries => const <OverlayEntry>[];

  /// Called when the route is inserted into the navigator.
23
  ///
Hixie's avatar
Hixie committed
24 25 26 27
  /// Use this to populate overlayEntries and add them to the overlay
  /// (accessible as navigator.overlay). (The reason the Route is responsible
  /// for doing this, rather than the Navigator, is that the Route will be
  /// responsible for _removing_ the entries and this way it's symmetric.)
28 29
  ///
  /// The overlay argument will be null if this is the first route inserted.
Hixie's avatar
Hixie committed
30
  void install(OverlayEntry insertionPoint) { }
31 32 33

  /// Called after install() when the route is pushed onto the navigator.
  void didPush() { }
Hixie's avatar
Hixie committed
34

35
  /// Called after install() when the route replaced another in the navigator.
36
  void didReplace(Route<dynamic> oldRoute) { }
37

Hixie's avatar
Hixie committed
38 39 40 41
  /// A request was made to pop this route. If the route can handle it
  /// internally (e.g. because it has its own stack of internal state) then
  /// return false, otherwise return true. Returning false will prevent the
  /// default behavior of NavigatorState.pop().
42 43 44
  ///
  /// If this is called, the Navigator will not call dispose(). It is the
  /// responsibility of the Route to later call dispose().
Hixie's avatar
Hixie committed
45
  bool didPop(T result) => true;
46

Hixie's avatar
Hixie committed
47 48 49
  /// Whether calling didPop() would return false.
  bool get willHandlePopInternally => false;

50
  /// The given route, which came after this one, has been popped off the
51
  /// navigator.
52
  void didPopNext(Route<dynamic> nextRoute) { }
53

Hixie's avatar
Hixie committed
54 55 56 57
  /// This route's next route has changed to the given new route. This is called
  /// on a route whenever the next route changes for any reason, except for
  /// cases when didPopNext() would be called, so long as it is in the history.
  /// nextRoute will be null if there's no next route.
58
  void didChangeNext(Route<dynamic> nextRoute) { }
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73
  /// The route should remove its overlays and free any other resources.
  ///
  /// A call to didPop() implies that the Route should call dispose() itself,
  /// but it is possible for dispose() to be called directly (e.g. if the route
  /// is replaced, or if the navigator itself is disposed).
  void dispose() { }

  /// Whether this route is the top-most route on the navigator.
  bool get isCurrent {
    if (_navigator == null)
      return false;
    assert(_navigator._history.contains(this));
    return _navigator._history.last == this;
  }
Adam Barth's avatar
Adam Barth committed
74 75
}

76
/// Data that might be useful in constructing a [Route].
77 78
class RouteSettings {
  const RouteSettings({
79 80 81 82 83
    this.name,
    this.mostValuableKeys,
    this.isInitialRoute: false
  });

84
  /// The name of the route (e.g., "/settings").
Adam Barth's avatar
Adam Barth committed
85
  final String name;
86 87 88 89 90 91 92

  /// The set of keys that are most relevant for constructoring [Hero]
  /// transitions. For example, if the current route contains a list of music
  /// albums and the user triggered this navigation by tapping one of the
  /// albums, the most valuable album cover is the one associated with the album
  /// the user tapped and is the one that should heroically transition when
  /// opening the details page for that album.
Adam Barth's avatar
Adam Barth committed
93
  final Set<Key> mostValuableKeys;
94 95 96 97

  /// Whether this route is the very first route being pushed onto this [Navigator].
  ///
  /// The initial route typically skips any entrance transition to speed startup.
98
  final bool isInitialRoute;
99

100
  @override
101 102 103 104 105 106 107 108 109
  String toString() {
    String result = '"$name"';
    if (mostValuableKeys != null && mostValuableKeys.isNotEmpty) {
      result += '; keys:';
      for (Key key in mostValuableKeys)
        result += ' $key';
    }
    return result;
  }
Adam Barth's avatar
Adam Barth committed
110 111
}

112
/// Creates a route for the given route settings.
113
typedef Route<dynamic> RouteFactory(RouteSettings settings);
114 115

/// A callback in during which you can perform a number of navigator operations (e.g., pop, push) that happen atomically.
Hixie's avatar
Hixie committed
116
typedef void NavigatorTransactionCallback(NavigatorTransaction transaction);
117

118
/// An interface for observing the behavior of a [Navigator].
119
class NavigatorObserver {
120
  /// The navigator that the observer is observing, if any.
121
  NavigatorState get navigator => _navigator;
122
  NavigatorState _navigator;
123 124

  /// The [Navigator] pushed the given route.
125
  void didPush(Route<dynamic> route, Route<dynamic> previousRoute) { }
126 127

  /// THe [Navigator] popped the given route.
128
  void didPop(Route<dynamic> route, Route<dynamic> previousRoute) { }
129 130
}

131 132 133 134 135 136 137 138
/// Manages a set of child widgets with a stack discipline.
///
/// Many apps have a navigator near the top of their widget hierarchy in order
/// to display their logical history using an [Overlay] with the most recently
/// visited pages visually on top of the older pages. Using this pattern lets
/// the navigator visually transition from one page to another by the widgets
/// around in the overlay. Similarly, the navigator can be used to show a dialog
/// by positioning the dialog widget above the current page.
139
class Navigator extends StatefulWidget {
140 141
  Navigator({
    Key key,
142
    this.initialRoute,
Adam Barth's avatar
Adam Barth committed
143
    this.onGenerateRoute,
144 145
    this.onUnknownRoute,
    this.observer
146
  }) : super(key: key) {
Adam Barth's avatar
Adam Barth committed
147
    assert(onGenerateRoute != null);
148 149
  }

150
  /// The name of the first route to show.
151
  final String initialRoute;
152 153

  /// Called to generate a route for a given [RouteSettings].
Adam Barth's avatar
Adam Barth committed
154
  final RouteFactory onGenerateRoute;
155 156 157 158 159 160 161 162 163

  /// Called when [onGenerateRoute] fails to generate a route.
  ///
  /// This callback is typically used for error handling. For example, this
  /// callback might always generate a "not found" page that describes the route
  /// that wasn't found.
  ///
  /// Unknown routes can arise either from errors in the app or from external
  /// requests to push routes, such as from Android intents.
Adam Barth's avatar
Adam Barth committed
164
  final RouteFactory onUnknownRoute;
165 166

  /// An observer for this navigator.
167
  final NavigatorObserver observer;
Adam Barth's avatar
Adam Barth committed
168

169
  /// The default name for the initial route.
Adam Barth's avatar
Adam Barth committed
170
  static const String defaultRouteName = '/';
171

172 173 174 175 176 177 178
  /// Push a named route onto the navigator that most tightly encloses the given context.
  ///
  /// The route name will be passed to that navigator's [onGenerateRoute]
  /// callback. The returned route will be pushed into the navigator. The set of
  /// most valuable keys will be used to construct an appropriate [Hero] transition.
  ///
  /// Uses [openTransaction()]. Only one transaction will be executed per frame.
Hixie's avatar
Hixie committed
179 180 181 182 183 184
  static void pushNamed(BuildContext context, String routeName, { Set<Key> mostValuableKeys }) {
    openTransaction(context, (NavigatorTransaction transaction) {
      transaction.pushNamed(routeName, mostValuableKeys: mostValuableKeys);
    });
  }

185 186 187 188 189 190 191 192
  /// Push a route onto the navigator that most tightly encloses the given context.
  ///
  /// Adds the given route to the Navigator's history, and transitions to it.
  /// The route will have didPush() and didChangeNext() called on it; the
  /// previous route, if any, will have didChangeNext() called on it; and the
  /// Navigator observer, if any, will have didPush() called on it.
  ///
  /// Uses [openTransaction()]. Only one transaction will be executed per frame.
193
  static void push(BuildContext context, Route<dynamic> route) {
Hixie's avatar
Hixie committed
194
    openTransaction(context, (NavigatorTransaction transaction) {
195
      transaction.push(route);
Hixie's avatar
Hixie committed
196 197 198
    });
  }

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
  /// Pop a route off the navigator that most tightly encloses the given context.
  ///
  /// Tries to removes the current route, calling its didPop() method. If that
  /// method returns false, then nothing else happens. Otherwise, the observer
  /// (if any) is notified using its didPop() method, and the previous route is
  /// notified using [Route.didChangeNext].
  ///
  /// If non-null, [result] will be used as the result of the route. Routes
  /// such as dialogs or popup menus typically use this mechanism to return the
  /// value selected by the user to the widget that created their route. The
  /// type of [result], if provided, must match the type argument of the class
  /// of the current route. (In practice, this is usually "dynamic".)
  ///
  /// Returns true if a route was popped; returns false if there are no further
  /// previous routes.
  ///
  /// Uses [openTransaction()]. Only one transaction will be executed per frame.
Hixie's avatar
Hixie committed
216 217 218 219 220 221 222
  static bool pop(BuildContext context, [ dynamic result ]) {
    bool returnValue;
    openTransaction(context, (NavigatorTransaction transaction) {
      returnValue = transaction.pop(result);
    });
    return returnValue;
  }
223

224 225 226 227
  /// Calls pop() repeatedly until the given route is the current route.
  /// If it is already the current route, nothing happens.
  ///
  /// Uses [openTransaction()]. Only one transaction will be executed per frame.
228
  static void popUntil(BuildContext context, Route<dynamic> targetRoute) {
Hixie's avatar
Hixie committed
229 230 231 232
    openTransaction(context, (NavigatorTransaction transaction) {
      transaction.popUntil(targetRoute);
    });
  }
Hixie's avatar
Hixie committed
233

234 235 236 237 238
  /// Whether the navigator that most tightly encloses the given context can be popped.
  ///
  /// The initial route cannot be popped off the navigator, which implies that
  /// this function returns true only if popping the navigator would not remove
  /// the initial route.
Hixie's avatar
Hixie committed
239
  static bool canPop(BuildContext context) {
Ian Hickson's avatar
Ian Hickson committed
240
    NavigatorState navigator = context.ancestorStateOfType(const TypeMatcher<NavigatorState>());
241
    return navigator != null && navigator.canPop();
Hixie's avatar
Hixie committed
242 243
  }

244 245 246 247
  /// Executes a simple transaction that both pops the current route off and
  /// pushes a named route into the navigator that most tightly encloses the given context.
  ///
  /// Uses [openTransaction()]. Only one transaction will be executed per frame.
Hixie's avatar
Hixie committed
248 249 250 251 252 253 254
  static void popAndPushNamed(BuildContext context, String routeName, { Set<Key> mostValuableKeys }) {
    openTransaction(context, (NavigatorTransaction transaction) {
      transaction.pop();
      transaction.pushNamed(routeName, mostValuableKeys: mostValuableKeys);
    });
  }

255 256 257 258 259 260
  /// Calls callback immediately to create a navigator transaction.
  ///
  /// To avoid race conditions, a navigator will execute at most one operation
  /// per animation frame. If you wish to perform a compound change to the
  /// navigator's state, you can use a navigator transaction to execute all the
  /// changes atomically by making the changes inside the given callback.
Hixie's avatar
Hixie committed
261
  static void openTransaction(BuildContext context, NavigatorTransactionCallback callback) {
Ian Hickson's avatar
Ian Hickson committed
262
    NavigatorState navigator = context.ancestorStateOfType(const TypeMatcher<NavigatorState>());
263
    assert(() {
264 265 266 267 268 269
      if (navigator == null) {
        throw new WidgetError(
          'openTransaction called with a context that does not include a Navigator.\n'
          'The context passed to the Navigator.openTransaction() method must be that of a widget that is a descendant of a Navigator widget.'
        );
      }
270 271
      return true;
    });
Hixie's avatar
Hixie committed
272 273
    navigator.openTransaction(callback);
  }
Adam Barth's avatar
Adam Barth committed
274

275
  @override
276
  NavigatorState createState() => new NavigatorState();
277 278
}

279
/// The state for a [Navigator] widget.
280
class NavigatorState extends State<Navigator> {
Adam Barth's avatar
Adam Barth committed
281
  final GlobalKey<OverlayState> _overlayKey = new GlobalKey<OverlayState>();
282
  final List<Route<dynamic>> _history = new List<Route<dynamic>>();
283

284
  @override
285 286
  void initState() {
    super.initState();
287 288
    assert(config.observer == null || config.observer.navigator == null);
    config.observer?._navigator = this;
289
    _push(config.onGenerateRoute(new RouteSettings(
290 291
      name: config.initialRoute ?? Navigator.defaultRouteName,
      isInitialRoute: true
292
    )));
293 294
  }

295
  @override
296 297 298 299 300 301 302 303
  void didUpdateConfig(Navigator oldConfig) {
    if (oldConfig.observer != config.observer) {
      oldConfig.observer?._navigator = null;
      assert(config.observer == null || config.observer.navigator == null);
      config.observer?._navigator = this;
    }
  }

304
  @override
305
  void dispose() {
306 307
    assert(!_debugLocked);
    assert(() { _debugLocked = true; return true; });
308
    config.observer?._navigator = null;
309
    for (Route<dynamic> route in _history) {
310 311 312
      route.dispose();
      route._navigator = null;
    }
313
    super.dispose();
314
    assert(() { _debugLocked = false; return true; });
315 316
  }

317
  /// The overlay this navigator uses for its visual presentation.
Adam Barth's avatar
Adam Barth committed
318
  OverlayState get overlay => _overlayKey.currentState;
319

Hixie's avatar
Hixie committed
320
  OverlayEntry get _currentOverlayEntry {
321
    for (Route<dynamic> route in _history.reversed) {
Adam Barth's avatar
Adam Barth committed
322 323
      if (route.overlayEntries.isNotEmpty)
        return route.overlayEntries.last;
324
    }
Adam Barth's avatar
Adam Barth committed
325
    return null;
326 327
  }

328 329
  bool _debugLocked = false; // used to prevent re-entrant calls to push, pop, and friends

Hixie's avatar
Hixie committed
330
  void _pushNamed(String name, { Set<Key> mostValuableKeys }) {
331
    assert(!_debugLocked);
332
    assert(name != null);
333
    RouteSettings settings = new RouteSettings(
Adam Barth's avatar
Adam Barth committed
334 335
      name: name,
      mostValuableKeys: mostValuableKeys
Hixie's avatar
Hixie committed
336
    );
337
    Route<dynamic> route = config.onGenerateRoute(settings);
338 339 340 341 342 343
    if (route == null) {
      assert(config.onUnknownRoute != null);
      route = config.onUnknownRoute(settings);
      assert(route != null);
    }
    _push(route);
344 345
  }

346
  void _push(Route<dynamic> route) {
347 348 349 350
    assert(!_debugLocked);
    assert(() { _debugLocked = true; return true; });
    assert(route != null);
    assert(route._navigator == null);
Hixie's avatar
Hixie committed
351
    setState(() {
352
      Route<dynamic> oldRoute = _history.isNotEmpty ? _history.last : null;
353
      route._navigator = this;
Hixie's avatar
Hixie committed
354
      route.install(_currentOverlayEntry);
355
      _history.add(route);
356
      route.didPush();
Hixie's avatar
Hixie committed
357
      route.didChangeNext(null);
358
      if (oldRoute != null)
Hixie's avatar
Hixie committed
359
        oldRoute.didChangeNext(route);
360
      config.observer?.didPush(route, oldRoute);
Hixie's avatar
Hixie committed
361
    });
362
    assert(() { _debugLocked = false; return true; });
Hixie's avatar
Hixie committed
363 364
  }

365
  void _replace({ Route<dynamic> oldRoute, Route<dynamic> newRoute }) {
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    assert(!_debugLocked);
    assert(oldRoute != null);
    assert(newRoute != null);
    if (oldRoute == newRoute)
      return;
    assert(() { _debugLocked = true; return true; });
    assert(oldRoute._navigator == this);
    assert(newRoute._navigator == null);
    assert(oldRoute.overlayEntries.isNotEmpty);
    assert(newRoute.overlayEntries.isEmpty);
    assert(!overlay.debugIsVisible(oldRoute.overlayEntries.last));
    setState(() {
      int index = _history.indexOf(oldRoute);
      assert(index >= 0);
      newRoute._navigator = this;
Hixie's avatar
Hixie committed
381
      newRoute.install(oldRoute.overlayEntries.last);
382 383
      _history[index] = newRoute;
      newRoute.didReplace(oldRoute);
Hixie's avatar
Hixie committed
384 385 386 387
      if (index + 1 < _history.length)
        newRoute.didChangeNext(_history[index + 1]);
      else
        newRoute.didChangeNext(null);
388
      if (index > 0)
Hixie's avatar
Hixie committed
389
        _history[index - 1].didChangeNext(newRoute);
390 391 392 393 394 395
      oldRoute.dispose();
      oldRoute._navigator = null;
    });
    assert(() { _debugLocked = false; return true; });
  }

396
  void _replaceRouteBefore({ Route<dynamic> anchorRoute, Route<dynamic> newRoute }) {
397 398 399
    assert(anchorRoute != null);
    assert(anchorRoute._navigator == this);
    assert(_history.indexOf(anchorRoute) > 0);
Hixie's avatar
Hixie committed
400
    _replace(oldRoute: _history[_history.indexOf(anchorRoute)-1], newRoute: newRoute);
401
  }
402

403
  void _removeRouteBefore(Route<dynamic> anchorRoute) {
404 405 406 407 408
    assert(!_debugLocked);
    assert(() { _debugLocked = true; return true; });
    assert(anchorRoute._navigator == this);
    int index = _history.indexOf(anchorRoute) - 1;
    assert(index >= 0);
409
    Route<dynamic> targetRoute = _history[index];
410 411 412 413
    assert(targetRoute._navigator == this);
    assert(targetRoute.overlayEntries.isEmpty || !overlay.debugIsVisible(targetRoute.overlayEntries.last));
    setState(() {
      _history.removeAt(index);
414
      Route<dynamic> newRoute = index < _history.length ? _history[index] : null;
Hixie's avatar
Hixie committed
415 416
      if (index > 0)
        _history[index - 1].didChangeNext(newRoute);
417 418 419 420 421 422
      targetRoute.dispose();
      targetRoute._navigator = null;
    });
    assert(() { _debugLocked = false; return true; });
  }

Hixie's avatar
Hixie committed
423
  bool _pop([dynamic result]) {
424 425
    assert(!_debugLocked);
    assert(() { _debugLocked = true; return true; });
426
    Route<dynamic> route = _history.last;
Hixie's avatar
Hixie committed
427
    assert(route._navigator == this);
Hixie's avatar
Hixie committed
428 429
    bool debugPredictedWouldPop;
    assert(() { debugPredictedWouldPop = !route.willHandlePopInternally; return true; });
Hixie's avatar
Hixie committed
430
    if (route.didPop(result)) {
Hixie's avatar
Hixie committed
431
      assert(debugPredictedWouldPop);
Hixie's avatar
Hixie committed
432 433
      if (_history.length > 1) {
        setState(() {
434 435 436
          // We use setState to guarantee that we'll rebuild, since the routes
          // can't do that for themselves, even if they have changed their own
          // state (e.g. ModalScope.isCurrent).
Hixie's avatar
Hixie committed
437
          _history.removeLast();
438 439
          _history.last.didPopNext(route);
          config.observer?.didPop(route, _history.last);
Hixie's avatar
Hixie committed
440 441 442 443 444 445
          route._navigator = null;
        });
      } else {
        assert(() { _debugLocked = false; return true; });
        return false;
      }
Hixie's avatar
Hixie committed
446 447
    } else {
      assert(!debugPredictedWouldPop);
Hixie's avatar
Hixie committed
448
    }
449
    assert(() { _debugLocked = false; return true; });
Hixie's avatar
Hixie committed
450
    return true;
Hixie's avatar
Hixie committed
451 452
  }

453
  void _popUntil(Route<dynamic> targetRoute) {
454 455
    assert(_history.contains(targetRoute));
    while (!targetRoute.isCurrent)
Hixie's avatar
Hixie committed
456 457 458
      _pop();
  }

459 460 461 462
  /// Whether this navigator can be popped.
  ///
  /// The only route that cannot be popped off the navigator is the initial
  /// route.
Hixie's avatar
Hixie committed
463 464 465 466 467
  bool canPop() {
    assert(_history.length > 0);
    return _history.length > 1 || _history[0].willHandlePopInternally;
  }

Hixie's avatar
Hixie committed
468 469
  bool _hadTransaction = true;

470 471 472 473 474 475
  /// Calls callback immediately to create a navigator transaction.
  ///
  /// To avoid race conditions, a navigator will execute at most one operation
  /// per animation frame. If you wish to perform a compound change to the
  /// navigator's state, you can use a navigator transaction to execute all the
  /// changes atomically by making the changes inside the given callback.
Hixie's avatar
Hixie committed
476 477 478 479 480 481 482 483 484 485 486
  bool openTransaction(NavigatorTransactionCallback callback) {
    assert(callback != null);
    if (_hadTransaction)
      return false;
    _hadTransaction = true;
    NavigatorTransaction transaction = new NavigatorTransaction._(this);
    setState(() {
      callback(transaction);
    });
    assert(() { transaction._debugClose(); return true; });
    return true;
487 488
  }

489
  @override
Adam Barth's avatar
Adam Barth committed
490
  Widget build(BuildContext context) {
491
    assert(!_debugLocked);
492
    assert(_history.isNotEmpty);
Hixie's avatar
Hixie committed
493
    _hadTransaction = false;
Adam Barth's avatar
Adam Barth committed
494 495
    return new Overlay(
      key: _overlayKey,
496
      initialEntries: _history.first.overlayEntries
497 498 499
    );
  }
}
Hixie's avatar
Hixie committed
500

501
/// A sequence of [Navigator] operations that are executed atomically.
Hixie's avatar
Hixie committed
502 503 504 505 506 507
class NavigatorTransaction {
  NavigatorTransaction._(this._navigator) {
    assert(_navigator != null);
  }
  NavigatorState _navigator;
  bool _debugOpen = true;
508

509 510 511
  /// The route name will be passed to the navigator's [onGenerateRoute]
  /// callback. The returned route will be pushed into the navigator. The set of
  /// most valuable keys will be used to construct an appropriate [Hero] transition.
Hixie's avatar
Hixie committed
512 513 514 515 516 517
  void pushNamed(String name, { Set<Key> mostValuableKeys }) {
    assert(_debugOpen);
    _navigator._pushNamed(name, mostValuableKeys: mostValuableKeys);
  }

  /// Adds the given route to the Navigator's history, and transitions to it.
Hixie's avatar
Hixie committed
518 519 520
  /// The route will have didPush() and didChangeNext() called on it; the
  /// previous route, if any, will have didChangeNext() called on it; and the
  /// Navigator observer, if any, will have didPush() called on it.
521
  void push(Route<dynamic> route) {
Hixie's avatar
Hixie committed
522
    assert(_debugOpen);
523
    _navigator._push(route);
Hixie's avatar
Hixie committed
524 525
  }

Hixie's avatar
Hixie committed
526 527
  /// Replaces one given route with another. Calls install(), didReplace(), and
  /// didChangeNext() on the new route, then dispose() on the old route. The
Hixie's avatar
Hixie committed
528 529 530 531 532 533 534 535 536
  /// navigator is not informed of the replacement.
  ///
  /// The old route must have overlay entries, otherwise we won't know where to
  /// insert the entries of the new route. The old route must not be currently
  /// visible (i.e. a later route have overlay entries that are currently
  /// opaque), otherwise the replacement would have a jarring effect.
  ///
  /// It is safe to call this redundantly (replacing a route with itself). Such
  /// calls are ignored.
537
  void replace({ Route<dynamic> oldRoute, Route<dynamic> newRoute }) {
Hixie's avatar
Hixie committed
538 539 540 541 542 543 544 545 546 547 548 549
    assert(_debugOpen);
    _navigator._replace(oldRoute: oldRoute, newRoute: newRoute);
  }

  /// Like replace(), but affects the route before the given anchorRoute rather
  /// than the anchorRoute itself.
  ///
  /// If newRoute is already the route before anchorRoute, then the call is
  /// ignored.
  ///
  /// The conditions described for [replace()] apply; for instance, the route
  /// before anchorRoute must have overlay entries.
550
  void replaceRouteBefore({ Route<dynamic> anchorRoute, Route<dynamic> newRoute }) {
Hixie's avatar
Hixie committed
551 552 553 554
    assert(_debugOpen);
    _navigator._replaceRouteBefore(anchorRoute: anchorRoute, newRoute: newRoute);
  }

Hixie's avatar
Hixie committed
555 556
  /// Removes the route prior to the given anchorRoute, and calls didChangeNext
  /// on the route prior to that one, if any. The observer is not notified.
557
  void removeRouteBefore(Route<dynamic> anchorRoute) {
Hixie's avatar
Hixie committed
558 559 560 561 562 563 564
    assert(_debugOpen);
    _navigator._removeRouteBefore(anchorRoute);
  }

  /// Tries to removes the current route, calling its didPop() method. If that
  /// method returns false, then nothing else happens. Otherwise, the observer
  /// (if any) is notified using its didPop() method, and the previous route is
Hixie's avatar
Hixie committed
565
  /// notified using [Route.didChangeNext].
Hixie's avatar
Hixie committed
566
  ///
567 568 569 570 571
  /// If non-null, [result] will be used as the result of the route. Routes
  /// such as dialogs or popup menus typically use this mechanism to return the
  /// value selected by the user to the widget that created their route. The
  /// type of [result], if provided, must match the type argument of the class
  /// of the current route. (In practice, this is usually "dynamic".)
Hixie's avatar
Hixie committed
572 573 574 575 576 577 578
  ///
  /// Returns true if a route was popped; returns false if there are no further
  /// previous routes.
  bool pop([dynamic result]) {
    assert(_debugOpen);
    return _navigator._pop(result);
  }
579

Hixie's avatar
Hixie committed
580 581
  /// Calls pop() repeatedly until the given route is the current route.
  /// If it is already the current route, nothing happens.
582
  void popUntil(Route<dynamic> targetRoute) {
Hixie's avatar
Hixie committed
583 584 585 586 587 588 589 590
    assert(_debugOpen);
    _navigator._popUntil(targetRoute);
  }

  void _debugClose() {
    assert(_debugOpen);
    _debugOpen = false;
  }
591
}