routes.dart 21.9 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
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 6
import 'dart:async';

7 8
import 'package:meta/meta.dart';

Adam Barth's avatar
Adam Barth committed
9
import 'basic.dart';
10
import 'focus.dart';
Adam Barth's avatar
Adam Barth committed
11
import 'framework.dart';
12
import 'modal_barrier.dart';
Adam Barth's avatar
Adam Barth committed
13 14
import 'navigator.dart';
import 'overlay.dart';
15
import 'page_storage.dart';
16
import 'pages.dart';
Adam Barth's avatar
Adam Barth committed
17

18
const Color _kTransparent = const Color(0x00000000);
Hixie's avatar
Hixie committed
19

20
/// A route that displays widgets in the [Navigator]'s [Overlay].
Hixie's avatar
Hixie committed
21
abstract class OverlayRoute<T> extends Route<T> {
22
  /// Subclasses should override this getter to return the builders for the overlay.
23
  Iterable<OverlayEntry> createOverlayEntries();
Adam Barth's avatar
Adam Barth committed
24

25
  /// The entries this route has placed in the overlay.
26
  @override
Adam Barth's avatar
Adam Barth committed
27
  List<OverlayEntry> get overlayEntries => _overlayEntries;
28
  final List<OverlayEntry> _overlayEntries = <OverlayEntry>[];
Adam Barth's avatar
Adam Barth committed
29

30
  @override
Hixie's avatar
Hixie committed
31
  void install(OverlayEntry insertionPoint) {
32
    assert(_overlayEntries.isEmpty);
33
    _overlayEntries.addAll(createOverlayEntries());
Hixie's avatar
Hixie committed
34
    navigator.overlay?.insertAll(_overlayEntries, above: insertionPoint);
35
    super.install(insertionPoint);
Adam Barth's avatar
Adam Barth committed
36 37
  }

38
  /// Controls whether [didPop] calls [finished].
39
  ///
40 41 42 43 44 45 46
  /// If true, this route removes its overlay entries during [didPop].
  /// Subclasses can override this getter if they want to delay the [finished]
  /// call (for example to animate the route's exit before removing it from the
  /// overlay).
  @protected
  bool get finishedWhenPopped => true;

47
  @override
Hixie's avatar
Hixie committed
48
  bool didPop(T result) {
49 50 51
    if (finishedWhenPopped)
      finished();
    return super.didPop(result);
Hixie's avatar
Hixie committed
52 53 54 55 56 57 58 59 60
  }

  /// Clears out the overlay entries.
  ///
  /// This method is intended to be used by subclasses who don't call
  /// super.didPop() because they want to have control over the timing of the
  /// overlay removal.
  ///
  /// Do not call this method outside of this context.
61
  @protected
Hixie's avatar
Hixie committed
62
  void finished() {
Adam Barth's avatar
Adam Barth committed
63 64 65 66
    for (OverlayEntry entry in _overlayEntries)
      entry.remove();
    _overlayEntries.clear();
  }
67

68
  @override
69 70
  void dispose() {
    finished();
71
    super.dispose();
72
  }
Adam Barth's avatar
Adam Barth committed
73 74
}

75
/// A route with entrance and exit transitions.
Hixie's avatar
Hixie committed
76
abstract class TransitionRoute<T> extends OverlayRoute<T> {
Hixie's avatar
Hixie committed
77 78
  /// This future completes only once the transition itself has finished, after
  /// the overlay entries have been removed from the navigator's overlay.
79 80 81 82
  ///
  /// This future completes once the animation has been dismissed. That will be
  /// after [popped], because [popped] completes before the animation even
  /// starts, as soon as the route is popped.
83 84
  Future<T> get completed => _transitionCompleter.future;
  final Completer<T> _transitionCompleter = new Completer<T>();
85

86
  /// The duration the transition lasts.
Adam Barth's avatar
Adam Barth committed
87
  Duration get transitionDuration;
88 89 90 91 92

  /// Whether the route obscures previous routes when the transition is complete.
  ///
  /// When an opaque route's entrance transition is complete, the routes behind
  /// the opaque route will not be built to save resources.
Adam Barth's avatar
Adam Barth committed
93 94
  bool get opaque;

95 96 97
  @override
  bool get finishedWhenPopped => false;

98 99
  /// The animation that drives the route's transition and the previous route's
  /// forward transition.
100 101
  Animation<double> get animation => _animation;
  Animation<double> _animation;
102

103 104 105
  /// The animation controller that the route uses to drive the transitions.
  ///
  /// The animation itself is exposed by the [animation] property.
106 107
  @protected
  AnimationController get controller => _controller;
108
  AnimationController _controller;
109

110
  /// Called to create the animation controller that will drive the transitions to
111 112
  /// this route from the previous one, and back to the previous route from this
  /// one.
113
  AnimationController createAnimationController() {
Adam Barth's avatar
Adam Barth committed
114 115
    Duration duration = transitionDuration;
    assert(duration != null && duration >= Duration.ZERO);
116 117 118 119 120
    return new AnimationController(
      duration: duration,
      debugLabel: debugLabel,
      vsync: navigator,
    );
Adam Barth's avatar
Adam Barth committed
121 122
  }

123 124
  /// Called to create the animation that exposes the current progress of
  /// the transition controlled by the animation controller created by
125
  /// [createAnimationController()].
126
  Animation<double> createAnimation() {
127 128
    assert(_controller != null);
    return _controller.view;
129 130
  }

Hixie's avatar
Hixie committed
131
  T _result;
Adam Barth's avatar
Adam Barth committed
132

133
  void _handleStatusChanged(AnimationStatus status) {
Adam Barth's avatar
Adam Barth committed
134
    switch (status) {
135
      case AnimationStatus.completed:
Adam Barth's avatar
Adam Barth committed
136 137 138
        if (overlayEntries.isNotEmpty)
          overlayEntries.first.opaque = opaque;
        break;
139 140
      case AnimationStatus.forward:
      case AnimationStatus.reverse:
Adam Barth's avatar
Adam Barth committed
141 142 143
        if (overlayEntries.isNotEmpty)
          overlayEntries.first.opaque = false;
        break;
144
      case AnimationStatus.dismissed:
Hixie's avatar
Hixie committed
145 146 147
        assert(!overlayEntries.first.opaque);
        finished(); // clear the overlays
        assert(overlayEntries.isEmpty);
Adam Barth's avatar
Adam Barth committed
148 149 150 151
        break;
    }
  }

152 153 154
  /// The animation for the route being pushed on top of this route. This
  /// animation lets this route coordinate with the entrance and exit transition
  /// of routes pushed on top of this route.
155
  Animation<double> get forwardAnimation => _forwardAnimation;
156
  final ProxyAnimation _forwardAnimation = new ProxyAnimation(kAlwaysDismissedAnimation);
Hixie's avatar
Hixie committed
157

158
  @override
Hixie's avatar
Hixie committed
159
  void install(OverlayEntry insertionPoint) {
160 161 162 163
    _controller = createAnimationController();
    assert(_controller != null);
    _animation = createAnimation();
    assert(_animation != null);
Hixie's avatar
Hixie committed
164
    super.install(insertionPoint);
165 166
  }

167
  @override
168
  void didPush() {
169
    _animation.addStatusListener(_handleStatusChanged);
170
    _controller.forward();
171
    super.didPush();
Adam Barth's avatar
Adam Barth committed
172 173
  }

174
  @override
175 176
  void didReplace(Route<dynamic> oldRoute) {
    if (oldRoute is TransitionRoute<dynamic>)
177
      _controller.value = oldRoute._controller.value;
178
    _animation.addStatusListener(_handleStatusChanged);
179 180 181
    super.didReplace(oldRoute);
  }

182
  @override
Hixie's avatar
Hixie committed
183
  bool didPop(T result) {
Adam Barth's avatar
Adam Barth committed
184
    _result = result;
185
    _controller.reverse();
186
    return super.didPop(result);
Hixie's avatar
Hixie committed
187 188
  }

189
  @override
190
  void didPopNext(Route<dynamic> nextRoute) {
191
    _updateForwardAnimation(nextRoute);
Hixie's avatar
Hixie committed
192
    super.didPopNext(nextRoute);
Adam Barth's avatar
Adam Barth committed
193 194
  }

195
  @override
196
  void didChangeNext(Route<dynamic> nextRoute) {
197
    _updateForwardAnimation(nextRoute);
Hixie's avatar
Hixie committed
198
    super.didChangeNext(nextRoute);
199 200
  }

201 202
  void _updateForwardAnimation(Route<dynamic> nextRoute) {
    if (nextRoute is TransitionRoute<dynamic> && canTransitionTo(nextRoute) && nextRoute.canTransitionFrom(this)) {
203
      Animation<double> current = _forwardAnimation.parent;
Hixie's avatar
Hixie committed
204
      if (current != null) {
205
        if (current is TrainHoppingAnimation) {
206 207
          TrainHoppingAnimation newAnimation;
          newAnimation = new TrainHoppingAnimation(
Hixie's avatar
Hixie committed
208
            current.currentTrain,
209
            nextRoute._animation,
Hixie's avatar
Hixie committed
210
            onSwitchedTrain: () {
211
              assert(_forwardAnimation.parent == newAnimation);
212
              assert(newAnimation.currentTrain == nextRoute._animation);
213
              _forwardAnimation.parent = newAnimation.currentTrain;
214
              newAnimation.dispose();
Hixie's avatar
Hixie committed
215 216
            }
          );
217
          _forwardAnimation.parent = newAnimation;
Hixie's avatar
Hixie committed
218 219
          current.dispose();
        } else {
220
          _forwardAnimation.parent = new TrainHoppingAnimation(current, nextRoute._animation);
Hixie's avatar
Hixie committed
221 222
        }
      } else {
223
        _forwardAnimation.parent = nextRoute._animation;
Hixie's avatar
Hixie committed
224
      }
Hixie's avatar
Hixie committed
225
    } else {
226
      _forwardAnimation.parent = kAlwaysDismissedAnimation;
Hixie's avatar
Hixie committed
227 228 229
    }
  }

230 231
  /// Whether this route can perform a transition to the given route.
  ///
232
  /// Subclasses can override this method to restrict the set of routes they
233
  /// need to coordinate transitions with.
234
  bool canTransitionTo(TransitionRoute<dynamic> nextRoute) => true;
235 236 237

  /// Whether this route can perform a transition from the given route.
  ///
238
  /// Subclasses can override this method to restrict the set of routes they
239
  /// need to coordinate transitions with.
240
  bool canTransitionFrom(TransitionRoute<dynamic> nextRoute) => true;
241

242
  @override
Hixie's avatar
Hixie committed
243 244
  void finished() {
    super.finished();
245
    _transitionCompleter.complete(_result);
Hixie's avatar
Hixie committed
246 247
  }

248
  @override
Hixie's avatar
Hixie committed
249
  void dispose() {
250
    _controller.dispose();
Hixie's avatar
Hixie committed
251 252 253
    super.dispose();
  }

254
  /// A short description of this route useful for debugging.
Adam Barth's avatar
Adam Barth committed
255
  String get debugLabel => '$runtimeType';
256 257

  @override
258
  String toString() => '$runtimeType(animation: $_controller)';
Adam Barth's avatar
Adam Barth committed
259
}
260

261
/// An entry in the history of a [LocalHistoryRoute].
Hixie's avatar
Hixie committed
262
class LocalHistoryEntry {
263
  /// Creates an entry in the history of a [LocalHistoryRoute].
Hixie's avatar
Hixie committed
264
  LocalHistoryEntry({ this.onRemove });
265 266

  /// Called when this entry is removed from the history of its associated [LocalHistoryRoute].
Hixie's avatar
Hixie committed
267
  final VoidCallback onRemove;
268

269
  LocalHistoryRoute<dynamic> _owner;
270 271

  /// Remove this entry from the history of its associated [LocalHistoryRoute].
Hixie's avatar
Hixie committed
272 273
  void remove() {
    _owner.removeLocalHistoryEntry(this);
274
    assert(_owner == null);
Hixie's avatar
Hixie committed
275
  }
276

Hixie's avatar
Hixie committed
277 278 279 280 281 282
  void _notifyRemoved() {
    if (onRemove != null)
      onRemove();
  }
}

283 284 285 286 287 288 289
/// A route that can handle back navigations internally by popping a list.
///
/// When a [Navigator] is instructed to pop, the current route is given an
/// opportunity to handle the pop internally. A LocalHistoryRoute handles the
/// pop internally if its list of local history entries is non-empty. Rather
/// than being removed as the current route, the most recent [LocalHistoryEntry]
/// is removed from the list and its [onRemove] is called.
290
abstract class LocalHistoryRoute<T> extends Route<T> {
Hixie's avatar
Hixie committed
291
  List<LocalHistoryEntry> _localHistory;
292 293 294

  /// Adds a local history entry to this route.
  ///
295
  /// When asked to pop, if this route has any local history entries, this route
296 297 298 299 300
  /// will handle the pop internally by removing the most recently added local
  /// history entry.
  ///
  /// The given local history entry must not already be part of another local
  /// history route.
Hixie's avatar
Hixie committed
301 302 303 304 305 306
  void addLocalHistoryEntry(LocalHistoryEntry entry) {
    assert(entry._owner == null);
    entry._owner = this;
    _localHistory ??= <LocalHistoryEntry>[];
    _localHistory.add(entry);
  }
307 308 309 310

  /// Remove a local history entry from this route.
  ///
  /// The entry's [onRemove] callback, if any, will be called synchronously.
Hixie's avatar
Hixie committed
311 312 313 314 315 316 317 318
  void removeLocalHistoryEntry(LocalHistoryEntry entry) {
    assert(entry != null);
    assert(entry._owner == this);
    assert(_localHistory.contains(entry));
    _localHistory.remove(entry);
    entry._owner = null;
    entry._notifyRemoved();
  }
319 320

  @override
Hixie's avatar
Hixie committed
321 322 323 324 325 326 327 328 329 330
  bool didPop(T result) {
    if (_localHistory != null && _localHistory.length > 0) {
      LocalHistoryEntry entry = _localHistory.removeLast();
      assert(entry._owner == this);
      entry._owner = null;
      entry._notifyRemoved();
      return false;
    }
    return super.didPop(result);
  }
331 332

  @override
Hixie's avatar
Hixie committed
333 334 335
  bool get willHandlePopInternally {
    return _localHistory != null && _localHistory.length > 0;
  }
Hixie's avatar
Hixie committed
336 337
}

Hixie's avatar
Hixie committed
338 339 340
class _ModalScopeStatus extends InheritedWidget {
  _ModalScopeStatus({
    Key key,
341
    this.isCurrent,
Hixie's avatar
Hixie committed
342 343 344
    this.route,
    Widget child
  }) : super(key: key, child: child) {
345
    assert(isCurrent != null);
Hixie's avatar
Hixie committed
346 347 348 349
    assert(route != null);
    assert(child != null);
  }

350
  final bool isCurrent;
351
  final Route<dynamic> route;
Hixie's avatar
Hixie committed
352

353
  @override
Hixie's avatar
Hixie committed
354
  bool updateShouldNotify(_ModalScopeStatus old) {
355
    return isCurrent != old.isCurrent ||
Hixie's avatar
Hixie committed
356 357 358
           route != old.route;
  }

359
  @override
Hixie's avatar
Hixie committed
360 361
  void debugFillDescription(List<String> description) {
    super.debugFillDescription(description);
362
    description.add('${isCurrent ? "active" : "inactive"}');
Hixie's avatar
Hixie committed
363 364 365
  }
}

366
class _ModalScope extends StatefulWidget {
367 368
  _ModalScope({
    Key key,
369 370
    this.route,
    this.page
371
  }) : super(key: key);
372

373
  final ModalRoute<dynamic> route;
374
  final Widget page;
375

376
  @override
377 378 379 380
  _ModalScopeState createState() => new _ModalScopeState();
}

class _ModalScopeState extends State<_ModalScope> {
381
  @override
382 383
  void initState() {
    super.initState();
384 385
    config.route.animation?.addStatusListener(_animationStatusChanged);
    config.route.forwardAnimation?.addStatusListener(_animationStatusChanged);
386 387
  }

388
  @override
389
  void didUpdateConfig(_ModalScope oldConfig) {
390
    assert(config.route == oldConfig.route);
391 392
  }

393
  @override
394
  void dispose() {
395 396
    config.route.animation?.removeStatusListener(_animationStatusChanged);
    config.route.forwardAnimation?.removeStatusListener(_animationStatusChanged);
397 398 399
    super.dispose();
  }

400
  void _animationStatusChanged(AnimationStatus status) {
401
    setState(() {
402
      // The animation's states are our build state, and they changed already.
403 404 405
    });
  }

406 407
  void _routeSetState(VoidCallback fn) {
    setState(fn);
408 409
  }

410
  @override
411
  Widget build(BuildContext context) {
412
    return new Focus(
413
      key: config.route.focusKey,
414
      child: new Offstage(
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
        offstage: config.route.offstage,
        child: new IgnorePointer(
          ignoring: config.route.animation?.status == AnimationStatus.reverse,
          child: config.route.buildTransitions(
            context,
            config.route.animation,
            config.route.forwardAnimation,
            new RepaintBoundary(
              child: new PageStorage(
                key: config.route._subtreeKey,
                bucket: config.route._storageBucket,
                child: new _ModalScopeStatus(
                  route: config.route,
                  isCurrent: config.route.isCurrent,
                  child: config.page
                )
              )
            )
          )
        )
      )
436
    );
437 438 439
  }
}

440
/// A route that blocks interaction with previous routes.
441 442 443 444
///
/// ModalRoutes cover the entire [Navigator]. They are not necessarily [opaque],
/// however; for example, a pop-up menu uses a ModalRoute but only shows the menu
/// in a small box overlapping the previous route.
445
abstract class ModalRoute<T> extends TransitionRoute<T> with LocalHistoryRoute<T> {
446
  /// Creates a route that blocks interaction with previous routes.
447
  ModalRoute({
448
    this.settings: const RouteSettings()
449
  });
450

Hixie's avatar
Hixie committed
451 452
  // The API for general users of this class

453 454 455
  /// The settings for this route.
  ///
  /// See [RouteSettings] for details.
456
  final RouteSettings settings;
457

458 459
  /// Returns the modal route most closely associated with the given context.
  ///
460
  /// Returns `null` if the given context is not associated with a modal route.
461 462 463 464 465 466
  ///
  /// Typical usage is as follows:
  ///
  /// ```dart
  /// ModalRoute<dynamic> route = ModalRoute.of(context);
  /// ```
467
  static ModalRoute<dynamic> of(BuildContext context) {
Ian Hickson's avatar
Ian Hickson committed
468
    _ModalScopeStatus widget = context.inheritFromWidgetOfExactType(_ModalScopeStatus);
Hixie's avatar
Hixie committed
469 470 471
    return widget?.route;
  }

472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
  /// Whenever you need to change internal state for a ModalRoute object, make
  /// the change in a function that you pass to setState(), as in:
  ///
  /// ```dart
  /// setState(() { myState = newValue });
  /// ```
  ///
  /// If you just change the state directly without calling setState(), then the
  /// route will not be scheduled for rebuilding, meaning that its rendering
  /// will not be updated.
  @protected
  void setState(VoidCallback fn) {
    if (_scopeKey.currentState != null) {
      _scopeKey.currentState._routeSetState(fn);
    } else {
      // The route isn't currently visible, so we don't have to call its setState
      // method, but we do still need to call the fn callback, otherwise the state
      // in the route won't be updated!
      fn();
    }
  }

Hans Muller's avatar
Hans Muller committed
494 495 496 497 498 499 500 501 502 503 504
  /// Returns a predicate that's true if the route has the specified name and if
  /// popping the route will not yield the same route, i.e. if the route's
  /// [willHandlePopInternally] property is false.
  ///
  /// This function is typically used with [Navigator.popUntil()].
  static RoutePredicate withName(String name) {
    return (Route<dynamic> route) {
      return !route.willHandlePopInternally
        && route is ModalRoute && route.settings.name == name;
    };
  }
505 506 507

  // The API for subclasses to override - used by _ModalScope

508
  /// Override this method to build the primary content of this route.
509 510 511 512 513 514 515 516
  ///
  /// * [context] The context in which the route is being built.
  /// * [animation] The animation for this route's transition. When entering,
  ///   the animation runs forward from 0.0 to 1.0. When exiting, this animation
  ///   runs backwards from 1.0 to 0.0.
  /// * [forwardAnimation] The animation for the route being pushed on top of
  ///   this route. This animation lets this route coordinate with the entrance
  ///   and exit transition of routes pushed on top of this route.
517
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> forwardAnimation);
518

519
  /// Override this method to wrap the route in a number of transition widgets.
520 521 522 523 524 525 526 527 528 529 530 531 532
  ///
  /// For example, to create a fade entrance transition, wrap the given child
  /// widget in a [FadeTransition] using the given animation as the opacity.
  ///
  /// By default, the child is not wrapped in any transition widgets.
  ///
  /// * [context] The context in which the route is being built.
  /// * [animation] The animation for this route's transition. When entering,
  ///   the animation runs forward from 0.0 to 1.0. When exiting, this animation
  ///   runs backwards from 1.0 to 0.0.
  /// * [forwardAnimation] The animation for the route being pushed on top of
  ///   this route. This animation lets this route coordinate with the entrance
  ///   and exit transition of routes pushed on top of this route.
533
  Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> forwardAnimation, Widget child) {
534 535 536
    return child;
  }

537 538 539
  @override
  GlobalKey get focusKey => new GlobalObjectKey(this);

540 541 542 543 544 545 546
  @override
  void install(OverlayEntry insertionPoint) {
    super.install(insertionPoint);
    _animationProxy = new ProxyAnimation(super.animation);
    _forwardAnimationProxy = new ProxyAnimation(super.forwardAnimation);
  }

547
  @override
548
  void didPush() {
549
    if (!settings.isInitialRoute) {
550 551 552 553 554 555 556 557 558 559 560
      BuildContext overlayContext = navigator.overlay?.context;
      assert(() {
        if (overlayContext == null) {
          throw new FlutterError(
            'Unable to find the BuildContext for the Navigator\'s overlay.\n'
            'Did you remember to pass the settings object to the route\'s '
            'constructor in your onGenerateRoute callback?'
          );
        }
        return true;
      });
561 562
      Focus.moveScopeTo(focusKey, context: overlayContext);
    }
563 564
    super.didPush();
  }
Hixie's avatar
Hixie committed
565

566 567 568 569 570 571
  @override
  void didPopNext(Route<dynamic> nextRoute) {
    Focus.moveScopeTo(focusKey, context: navigator.overlay.context);
    super.didPopNext(nextRoute);
  }

572 573
  // The API for subclasses to override - used by this class

Hixie's avatar
Hixie committed
574 575
  /// Whether you can dismiss this route by tapping the modal barrier.
  bool get barrierDismissable;
576

Hixie's avatar
Hixie committed
577 578 579
  /// The color to use for the modal barrier. If this is null, the barrier will
  /// be transparent.
  Color get barrierColor;
580

581 582 583 584 585 586 587
  /// Whether the route should remain in memory when it is inactive. If this is
  /// true, then the route is maintained, so that any futures it is holding from
  /// the next route will properly resolve when the next route pops. If this is
  /// not necessary, this can be set to false to allow the framework to entirely
  /// discard the route's widget hierarchy when it is not visible.
  bool get maintainState;

588 589 590

  // The API for _ModalScope and HeroController

591 592 593 594 595 596 597
  /// Whether this route is currently offstage.
  ///
  /// On the first frame of a route's entrance transition, the route is built
  /// [Offstage] using an animation progress of 1.0. The route is invisible and
  /// non-interactive, but each widget has its final size and position. This
  /// mechanism lets the [HeroController] determine the final local of any hero
  /// widgets being animated as part of the transition.
598 599
  bool get offstage => _offstage;
  bool _offstage = false;
600
  set offstage (bool value) {
601 602
    if (_offstage == value)
      return;
603 604 605
    setState(() {
      _offstage = value;
    });
606 607
    _animationProxy.parent = _offstage ? kAlwaysCompleteAnimation : super.animation;
    _forwardAnimationProxy.parent = _offstage ? kAlwaysDismissedAnimation : super.forwardAnimation;
608 609
  }

610
  /// The build context for the subtree containing the primary content of this route.
611 612
  BuildContext get subtreeContext => _subtreeKey.currentContext;

613 614 615 616 617 618 619 620
  @override
  Animation<double> get animation => _animationProxy;
  ProxyAnimation _animationProxy;

  @override
  Animation<double> get forwardAnimation => _forwardAnimationProxy;
  ProxyAnimation _forwardAnimationProxy;

621 622 623

  // Internals

624
  final GlobalKey<_ModalScopeState> _scopeKey = new GlobalKey<_ModalScopeState>();
625 626 627
  final GlobalKey _subtreeKey = new GlobalKey();
  final PageStorageBucket _storageBucket = new PageStorageBucket();

628
  // one of the builders
629
  Widget _buildModalBarrier(BuildContext context) {
630
    Widget barrier;
Hixie's avatar
Hixie committed
631 632
    if (barrierColor != null) {
      assert(barrierColor != _kTransparent);
633
      Animation<Color> color = new ColorTween(
634 635 636 637 638 639
        begin: _kTransparent,
        end: barrierColor
      ).animate(new CurvedAnimation(
        parent: animation,
        curve: Curves.ease
      ));
640
      barrier = new AnimatedModalBarrier(
641
        color: color,
Hixie's avatar
Hixie committed
642 643 644
        dismissable: barrierDismissable
      );
    } else {
645
      barrier = new ModalBarrier(dismissable: barrierDismissable);
Hixie's avatar
Hixie committed
646
    }
647
    assert(animation.status != AnimationStatus.dismissed);
648
    return new IgnorePointer(
649
      ignoring: animation.status == AnimationStatus.reverse,
650 651
      child: barrier
    );
652 653
  }

654
  // one of the builders
655 656 657
  Widget _buildModalScope(BuildContext context) {
    return new _ModalScope(
      key: _scopeKey,
658 659 660
      route: this,
      page: buildPage(context, animation, forwardAnimation)
      // _ModalScope calls buildTransitions(), defined above
661 662 663
    );
  }

664
  @override
665 666 667 668
  Iterable<OverlayEntry> createOverlayEntries() sync* {
    yield new OverlayEntry(builder: _buildModalBarrier);
    yield new OverlayEntry(builder: _buildModalScope, maintainState: maintainState);
  }
669

670
  @override
671
  String toString() => '$runtimeType($settings, animation: $_animation)';
672
}
Hixie's avatar
Hixie committed
673 674 675

/// A modal route that overlays a widget over the current route.
abstract class PopupRoute<T> extends ModalRoute<T> {
676
  @override
Hixie's avatar
Hixie committed
677
  bool get opaque => false;
678

679 680 681
  @override
  bool get maintainState => true;

682
  @override
683 684
  void didChangeNext(Route<dynamic> nextRoute) {
    assert(nextRoute is! PageRoute<dynamic>);
Hixie's avatar
Hixie committed
685
    super.didChangeNext(nextRoute);
686
  }
Hixie's avatar
Hixie committed
687
}