animations.dart 23.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'dart:math' as math;
8

9 10
import 'package:flutter/foundation.dart';

11 12
import 'animation.dart';
import 'curves.dart';
13 14
import 'listener_helpers.dart';

15 16 17
// Examples can assume:
// AnimationController controller;

18 19
class _AlwaysCompleteAnimation extends Animation<double> {
  const _AlwaysCompleteAnimation();
20

21
  @override
22
  void addListener(VoidCallback listener) { }
23 24

  @override
25
  void removeListener(VoidCallback listener) { }
26 27

  @override
28
  void addStatusListener(AnimationStatusListener listener) { }
29 30

  @override
31
  void removeStatusListener(AnimationStatusListener listener) { }
32 33

  @override
34
  AnimationStatus get status => AnimationStatus.completed;
35 36

  @override
37
  double get value => 1.0;
38 39 40

  @override
  String toString() => 'kAlwaysCompleteAnimation';
41 42
}

43 44 45 46 47
/// An animation that is always complete.
///
/// Using this constant involves less overhead than building an
/// [AnimationController] with an initial value of 1.0. This is useful when an
/// API expects an animation but you don't actually want to animate anything.
48
const Animation<double> kAlwaysCompleteAnimation = _AlwaysCompleteAnimation();
49

50 51
class _AlwaysDismissedAnimation extends Animation<double> {
  const _AlwaysDismissedAnimation();
52

53
  @override
54
  void addListener(VoidCallback listener) { }
55 56

  @override
57
  void removeListener(VoidCallback listener) { }
58 59

  @override
60
  void addStatusListener(AnimationStatusListener listener) { }
61 62

  @override
63
  void removeStatusListener(AnimationStatusListener listener) { }
64 65

  @override
66
  AnimationStatus get status => AnimationStatus.dismissed;
67 68

  @override
69
  double get value => 0.0;
70 71 72

  @override
  String toString() => 'kAlwaysDismissedAnimation';
73 74
}

75 76 77 78 79
/// An animation that is always dismissed.
///
/// Using this constant involves less overhead than building an
/// [AnimationController] with an initial value of 0.0. This is useful when an
/// API expects an animation but you don't actually want to animate anything.
80
const Animation<double> kAlwaysDismissedAnimation = _AlwaysDismissedAnimation();
81

82
/// An animation that is always stopped at a given value.
83 84
///
/// The [status] is always [AnimationStatus.forward].
85
class AlwaysStoppedAnimation<T> extends Animation<T> {
86 87 88
  /// Creates an [AlwaysStoppedAnimation] with the given value.
  ///
  /// Since the [value] and [status] of an [AlwaysStoppedAnimation] can never
89
  /// change, the listeners can never be called. It is therefore safe to reuse
90
  /// an [AlwaysStoppedAnimation] instance in multiple places. If the [value] to
91
  /// be used is known at compile time, the constructor should be called as a
92
  /// `const` constructor.
93 94
  const AlwaysStoppedAnimation(this.value);

95
  @override
96
  final T value;
97

98
  @override
99
  void addListener(VoidCallback listener) { }
100 101

  @override
102
  void removeListener(VoidCallback listener) { }
103 104

  @override
105
  void addStatusListener(AnimationStatusListener listener) { }
106 107

  @override
108
  void removeStatusListener(AnimationStatusListener listener) { }
109 110

  @override
111
  AnimationStatus get status => AnimationStatus.forward;
112 113 114 115 116

  @override
  String toStringDetails() {
    return '${super.toStringDetails()} $value; paused';
  }
117 118
}

119 120 121 122 123 124 125 126
/// Implements most of the [Animation] interface by deferring its behavior to a
/// given [parent] Animation.
///
/// To implement an [Animation] that is driven by a parent, it is only necessary
/// to mix in this class, implement [parent], and implement `T get value`.
///
/// To define a mapping from values in the range 0..1, consider subclassing
/// [Tween] instead.
127
mixin AnimationWithParentMixin<T> {
128 129 130
  /// The animation whose value this animation will proxy.
  ///
  /// This animation must remain the same for the lifetime of this object. If
131
  /// you wish to proxy a different animation at different times, consider using
132 133
  /// [ProxyAnimation].
  Animation<T> get parent;
134

135 136 137
  // keep these next five dartdocs in sync with the dartdocs in Animation<T>

  /// Calls the listener every time the value of the animation changes.
138 139
  ///
  /// Listeners can be removed with [removeListener].
140
  void addListener(VoidCallback listener) => parent.addListener(listener);
141 142

  /// Stop calling the listener every time the value of the animation changes.
143 144
  ///
  /// Listeners can be added with [addListener].
145
  void removeListener(VoidCallback listener) => parent.removeListener(listener);
146 147

  /// Calls listener every time the status of the animation changes.
148 149
  ///
  /// Listeners can be removed with [removeStatusListener].
150
  void addStatusListener(AnimationStatusListener listener) => parent.addStatusListener(listener);
151 152

  /// Stops calling the listener every time the status of the animation changes.
153 154
  ///
  /// Listeners can be added with [addStatusListener].
155 156
  void removeStatusListener(AnimationStatusListener listener) => parent.removeStatusListener(listener);

157
  /// The current status of this animation.
158 159 160
  AnimationStatus get status => parent.status;
}

161 162 163 164
/// An animation that is a proxy for another animation.
///
/// A proxy animation is useful because the parent animation can be mutated. For
/// example, one object can create a proxy animation, hand the proxy to another
165
/// object, and then later change the animation from which the proxy receives
166
/// its value.
167
class ProxyAnimation extends Animation<double>
168
  with AnimationLazyListenerMixin, AnimationLocalListenersMixin, AnimationLocalStatusListenersMixin {
169 170 171 172 173

  /// Creates a proxy animation.
  ///
  /// If the animation argument is omitted, the proxy animation will have the
  /// status [AnimationStatus.dismissed] and a value of 0.0.
174
  ProxyAnimation([Animation<double> animation]) {
175 176
    _parent = animation;
    if (_parent == null) {
177
      _status = AnimationStatus.dismissed;
178 179 180 181
      _value = 0.0;
    }
  }

182
  AnimationStatus _status;
183 184
  double _value;

185 186 187 188
  /// The animation whose value this animation will proxy.
  ///
  /// This value is mutable. When mutated, the listeners on the proxy animation
  /// will be transparently updated to be listening to the new parent animation.
189 190
  Animation<double> get parent => _parent;
  Animation<double> _parent;
191
  set parent(Animation<double> value) {
192
    if (value == _parent)
193
      return;
194 195 196
    if (_parent != null) {
      _status = _parent.status;
      _value = _parent.value;
197 198 199
      if (isListening)
        didStopListening();
    }
200 201
    _parent = value;
    if (_parent != null) {
202 203
      if (isListening)
        didStartListening();
204
      if (_value != _parent.value)
205
        notifyListeners();
206 207
      if (_status != _parent.status)
        notifyStatusListeners(_parent.status);
208 209 210 211 212
      _status = null;
      _value = null;
    }
  }

213
  @override
214
  void didStartListening() {
215 216 217
    if (_parent != null) {
      _parent.addListener(notifyListeners);
      _parent.addStatusListener(notifyStatusListeners);
218 219 220
    }
  }

221
  @override
222
  void didStopListening() {
223 224 225
    if (_parent != null) {
      _parent.removeListener(notifyListeners);
      _parent.removeStatusListener(notifyStatusListeners);
226 227 228
    }
  }

229
  @override
230
  AnimationStatus get status => _parent != null ? _parent.status : _status;
231 232

  @override
233
  double get value => _parent != null ? _parent.value : _value;
234 235 236 237

  @override
  String toString() {
    if (parent == null)
238 239
      return '${objectRuntimeType(this, 'ProxyAnimation')}(null; ${super.toStringDetails()} ${value.toStringAsFixed(3)})';
    return '$parent\u27A9${objectRuntimeType(this, 'ProxyAnimation')}';
240
  }
241 242
}

243 244 245
/// An animation that is the reverse of another animation.
///
/// If the parent animation is running forward from 0.0 to 1.0, this animation
246 247 248 249 250
/// is running in reverse from 1.0 to 0.0.
///
/// Using a [ReverseAnimation] is different from simply using a [Tween] with a
/// begin of 1.0 and an end of 0.0 because the tween does not change the status
/// or direction of the animation.
251 252 253 254 255 256 257
///
/// See also:
///
///  * [Curve.flipped] and [FlippedCurve], which provide a similar effect but on
///    [Curve]s.
///  * [CurvedAnimation], which can take separate curves for when the animation
///    is going forward than for when it is going in reverse.
258
class ReverseAnimation extends Animation<double>
259
  with AnimationLazyListenerMixin, AnimationLocalStatusListenersMixin {
260 261 262 263

  /// Creates a reverse animation.
  ///
  /// The parent argument must not be null.
264 265
  ReverseAnimation(this.parent)
    : assert(parent != null);
266

267
  /// The animation whose value and direction this animation is reversing.
268
  final Animation<double> parent;
269

270
  @override
271 272
  void addListener(VoidCallback listener) {
    didRegisterListener();
273
    parent.addListener(listener);
274
  }
275 276

  @override
277
  void removeListener(VoidCallback listener) {
278
    parent.removeListener(listener);
279 280 281
    didUnregisterListener();
  }

282
  @override
283
  void didStartListening() {
284
    parent.addStatusListener(_statusChangeHandler);
285 286
  }

287
  @override
288
  void didStopListening() {
289
    parent.removeStatusListener(_statusChangeHandler);
290 291
  }

292
  void _statusChangeHandler(AnimationStatus status) {
293 294 295
    notifyStatusListeners(_reverseStatus(status));
  }

296
  @override
297
  AnimationStatus get status => _reverseStatus(parent.status);
298 299

  @override
300
  double get value => 1.0 - parent.value;
301

302
  AnimationStatus _reverseStatus(AnimationStatus status) {
303
    assert(status != null);
304
    switch (status) {
305 306 307 308
      case AnimationStatus.forward: return AnimationStatus.reverse;
      case AnimationStatus.reverse: return AnimationStatus.forward;
      case AnimationStatus.completed: return AnimationStatus.dismissed;
      case AnimationStatus.dismissed: return AnimationStatus.completed;
309
    }
pq's avatar
pq committed
310
    return null;
311
  }
312 313 314

  @override
  String toString() {
315
    return '$parent\u27AA${objectRuntimeType(this, 'ReverseAnimation')}';
316
  }
317 318
}

319 320
/// An animation that applies a curve to another animation.
///
321
/// [CurvedAnimation] is useful when you want to apply a non-linear [Curve] to
322 323
/// an animation object, especially if you want different curves when the
/// animation is going forward vs when it is going backward.
324
///
325 326 327 328
/// Depending on the given curve, the output of the [CurvedAnimation] could have
/// a wider range than its input. For example, elastic curves such as
/// [Curves.elasticIn] will significantly overshoot or undershoot the default
/// range of 0.0 to 1.0.
329 330
///
/// If you want to apply a [Curve] to a [Tween], consider using [CurveTween].
331
///
332
/// {@tool snippet}
333 334 335 336 337 338 339 340 341 342
///
/// The following code snippet shows how you can apply a curve to a linear
/// animation produced by an [AnimationController] `controller`.
///
/// ```dart
/// final Animation<double> animation = CurvedAnimation(
///   parent: controller,
///   curve: Curves.ease,
/// );
/// ```
343
/// {@end-tool}
344
/// {@tool snippet}
345 346 347 348 349 350 351 352 353 354 355 356 357
///
/// This second code snippet shows how to apply a different curve in the forward
/// direction than in the reverse direction. This can't be done using a
/// [CurveTween] (since [Tween]s are not aware of the animation direction when
/// they are applied).
///
/// ```dart
/// final Animation<double> animation = CurvedAnimation(
///   parent: controller,
///   curve: Curves.easeIn,
///   reverseCurve: Curves.easeOut,
/// );
/// ```
358
/// {@end-tool}
359 360 361 362 363 364 365 366 367 368 369
///
/// By default, the [reverseCurve] matches the forward [curve].
///
/// See also:
///
///  * [CurveTween], for an alternative way of expressing the first sample
///    above.
///  * [AnimationController], for examples of creating and disposing of an
///    [AnimationController].
///  * [Curve.flipped] and [FlippedCurve], which provide the reverse of a
///    [Curve].
370
class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<double> {
371 372 373
  /// Creates a curved animation.
  ///
  /// The parent and curve arguments must not be null.
374
  CurvedAnimation({
375 376
    @required this.parent,
    @required this.curve,
377
    this.reverseCurve,
378 379
  }) : assert(parent != null),
       assert(curve != null) {
380 381
    _updateCurveDirection(parent.status);
    parent.addStatusListener(_updateCurveDirection);
382 383
  }

384
  /// The animation to which this animation applies a curve.
385
  @override
386 387 388 389 390 391 392
  final Animation<double> parent;

  /// The curve to use in the forward direction.
  Curve curve;

  /// The curve to use in the reverse direction.
  ///
393 394 395 396 397 398 399 400 401 402
  /// If the parent animation changes direction without first reaching the
  /// [AnimationStatus.completed] or [AnimationStatus.dismissed] status, the
  /// [CurvedAnimation] stays on the same curve (albeit in the opposite
  /// direction) to avoid visual discontinuities.
  ///
  /// If you use a non-null [reverseCurve], you might want to hold this object
  /// in a [State] object rather than recreating it each time your widget builds
  /// in order to take advantage of the state in this object that avoids visual
  /// discontinuities.
  ///
403 404 405 406 407 408 409
  /// If this field is null, uses [curve] in both directions.
  Curve reverseCurve;

  /// The direction used to select the current curve.
  ///
  /// The curve direction is only reset when we hit the beginning or the end of
  /// the timeline to avoid discontinuities in the value of any variables this
410
  /// animation is used to animate.
Adam Barth's avatar
Adam Barth committed
411
  AnimationStatus _curveDirection;
412

413
  void _updateCurveDirection(AnimationStatus status) {
414 415 416 417 418 419
    switch (status) {
      case AnimationStatus.dismissed:
      case AnimationStatus.completed:
        _curveDirection = null;
        break;
      case AnimationStatus.forward:
Adam Barth's avatar
Adam Barth committed
420
        _curveDirection ??= AnimationStatus.forward;
421 422
        break;
      case AnimationStatus.reverse:
Adam Barth's avatar
Adam Barth committed
423
        _curveDirection ??= AnimationStatus.reverse;
424 425 426 427
        break;
    }
  }

428 429 430 431
  bool get _useForwardCurve {
    return reverseCurve == null || (_curveDirection ?? parent.status) != AnimationStatus.reverse;
  }

432
  @override
433
  double get value {
434
    final Curve activeCurve = _useForwardCurve ? curve : reverseCurve;
435

436
    final double t = parent.value;
437 438 439
    if (activeCurve == null)
      return t;
    if (t == 0.0 || t == 1.0) {
440 441 442 443
      assert(() {
        final double transformedValue = activeCurve.transform(t);
        final double roundedTransformedValue = transformedValue.round().toDouble();
        if (roundedTransformedValue != t) {
444 445 446 447 448 449
          throw FlutterError(
            'Invalid curve endpoint at $t.\n'
            'Curves must map 0.0 to near zero and 1.0 to near one but '
            '${activeCurve.runtimeType} mapped $t to $transformedValue, which '
            'is near $roundedTransformedValue.'
          );
450 451
        }
        return true;
452
      }());
453 454 455 456
      return t;
    }
    return activeCurve.transform(t);
  }
457 458 459 460 461

  @override
  String toString() {
    if (reverseCurve == null)
      return '$parent\u27A9$curve';
462
    if (_useForwardCurve)
463
      return '$parent\u27A9$curve\u2092\u2099/$reverseCurve';
464 465
    return '$parent\u27A9$curve/$reverseCurve\u2092\u2099';
  }
466 467
}

468 469
enum _TrainHoppingMode { minimize, maximize }

470 471
/// This animation starts by proxying one animation, but when the value of that
/// animation crosses the value of the second (either because the second is
472
/// going in the opposite direction, or because the one overtakes the other),
473 474 475 476 477 478 479 480 481 482
/// the animation hops over to proxying the second animation.
///
/// When the [TrainHoppingAnimation] starts proxying the second animation
/// instead of the first, the [onSwitchedTrain] callback is called.
///
/// If the two animations start at the same value, then the
/// [TrainHoppingAnimation] immediately hops to the second animation, and the
/// [onSwitchedTrain] callback is not called. If only one animation is provided
/// (i.e. if the second is null), then the [TrainHoppingAnimation] just proxies
/// the first animation.
483 484 485 486 487
///
/// Since this object must track the two animations even when it has no
/// listeners of its own, instead of shutting down when all its listeners are
/// removed, it exposes a [dispose()] method. Call this method to shut this
/// object down.
488
class TrainHoppingAnimation extends Animation<double>
489
  with AnimationEagerListenerMixin, AnimationLocalListenersMixin, AnimationLocalStatusListenersMixin {
490 491 492 493

  /// Creates a train-hopping animation.
  ///
  /// The current train argument must not be null but the next train argument
494 495
  /// can be null. If the next train is null, then this object will just proxy
  /// the first animation and never hop.
496
  TrainHoppingAnimation(this._currentTrain, this._nextTrain, { this.onSwitchedTrain })
497
      : assert(_currentTrain != null) {
498
    if (_nextTrain != null) {
499 500 501 502
      if (_currentTrain.value == _nextTrain.value) {
        _currentTrain = _nextTrain;
        _nextTrain = null;
      } else if (_currentTrain.value > _nextTrain.value) {
503 504
        _mode = _TrainHoppingMode.maximize;
      } else {
505
        assert(_currentTrain.value < _nextTrain.value);
506 507 508 509 510
        _mode = _TrainHoppingMode.minimize;
      }
    }
    _currentTrain.addStatusListener(_statusChangeHandler);
    _currentTrain.addListener(_valueChangeHandler);
511
    _nextTrain?.addListener(_valueChangeHandler);
512
    assert(_mode != null || _nextTrain == null);
513 514
  }

515 516 517 518
  /// The animation that is currently driving this animation.
  ///
  /// The identity of this object will change from the first animation to the
  /// second animation when [onSwitchedTrain] is called.
519 520 521
  Animation<double> get currentTrain => _currentTrain;
  Animation<double> _currentTrain;
  Animation<double> _nextTrain;
522 523
  _TrainHoppingMode _mode;

524 525 526 527 528
  /// Called when this animation switches to be driven by the second animation.
  ///
  /// This is not called if the two animations provided to the constructor have
  /// the same value at the time of the call to the constructor. In that case,
  /// the second animation is used from the start, and the first is ignored.
529 530
  VoidCallback onSwitchedTrain;

531 532
  AnimationStatus _lastStatus;
  void _statusChangeHandler(AnimationStatus status) {
533 534 535 536 537 538 539 540
    assert(_currentTrain != null);
    if (status != _lastStatus) {
      notifyListeners();
      _lastStatus = status;
    }
    assert(_lastStatus != null);
  }

541
  @override
542
  AnimationStatus get status => _currentTrain.status;
543 544 545 546 547 548

  double _lastValue;
  void _valueChangeHandler() {
    assert(_currentTrain != null);
    bool hop = false;
    if (_nextTrain != null) {
549
      assert(_mode != null);
550 551 552 553 554 555 556 557 558
      switch (_mode) {
        case _TrainHoppingMode.minimize:
          hop = _nextTrain.value <= _currentTrain.value;
          break;
        case _TrainHoppingMode.maximize:
          hop = _nextTrain.value >= _currentTrain.value;
          break;
      }
      if (hop) {
559 560 561
        _currentTrain
          ..removeStatusListener(_statusChangeHandler)
          ..removeListener(_valueChangeHandler);
562
        _currentTrain = _nextTrain;
563 564 565
        _nextTrain = null;
        _currentTrain.addStatusListener(_statusChangeHandler);
        _statusChangeHandler(_currentTrain.status);
566 567
      }
    }
568
    final double newValue = value;
569 570 571 572 573 574 575 576 577
    if (newValue != _lastValue) {
      notifyListeners();
      _lastValue = newValue;
    }
    assert(_lastValue != null);
    if (hop && onSwitchedTrain != null)
      onSwitchedTrain();
  }

578
  @override
579 580 581 582
  double get value => _currentTrain.value;

  /// Frees all the resources used by this performance.
  /// After this is called, this object is no longer usable.
583
  @override
584 585 586 587 588
  void dispose() {
    assert(_currentTrain != null);
    _currentTrain.removeStatusListener(_statusChangeHandler);
    _currentTrain.removeListener(_valueChangeHandler);
    _currentTrain = null;
589 590
    _nextTrain?.removeListener(_valueChangeHandler);
    _nextTrain = null;
591
    super.dispose();
592
  }
593 594 595 596

  @override
  String toString() {
    if (_nextTrain != null)
597 598
      return '$currentTrain\u27A9${objectRuntimeType(this, 'TrainHoppingAnimation')}(next: $_nextTrain)';
    return '$currentTrain\u27A9${objectRuntimeType(this, 'TrainHoppingAnimation')}(no next)';
599
  }
600
}
601 602 603 604 605 606 607

/// An interface for combining multiple Animations. Subclasses need only
/// implement the `value` getter to control how the child animations are
/// combined. Can be chained to combine more than 2 animations.
///
/// For example, to create an animation that is the sum of two others, subclass
/// this class and define `T get value = first.value + second.value;`
608 609 610
///
/// By default, the [status] of a [CompoundAnimation] is the status of the
/// [next] animation if [next] is moving, and the status of the [first]
611
/// animation otherwise.
612 613 614 615 616
abstract class CompoundAnimation<T> extends Animation<T>
  with AnimationLazyListenerMixin, AnimationLocalListenersMixin, AnimationLocalStatusListenersMixin {
  /// Creates a CompoundAnimation. Both arguments must be non-null. Either can
  /// be a CompoundAnimation itself to combine multiple animations.
  CompoundAnimation({
617 618
    @required this.first,
    @required this.next,
619 620
  }) : assert(first != null),
       assert(next != null);
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

  /// The first sub-animation. Its status takes precedence if neither are
  /// animating.
  final Animation<T> first;

  /// The second sub-animation.
  final Animation<T> next;

  @override
  void didStartListening() {
    first.addListener(_maybeNotifyListeners);
    first.addStatusListener(_maybeNotifyStatusListeners);
    next.addListener(_maybeNotifyListeners);
    next.addStatusListener(_maybeNotifyStatusListeners);
  }

  @override
  void didStopListening() {
    first.removeListener(_maybeNotifyListeners);
    first.removeStatusListener(_maybeNotifyStatusListeners);
    next.removeListener(_maybeNotifyListeners);
    next.removeStatusListener(_maybeNotifyStatusListeners);
  }

645
  /// Gets the status of this animation based on the [first] and [next] status.
646
  ///
647 648
  /// The default is that if the [next] animation is moving, use its status.
  /// Otherwise, default to [first].
649 650 651 652 653 654 655 656 657
  @override
  AnimationStatus get status {
    if (next.status == AnimationStatus.forward || next.status == AnimationStatus.reverse)
      return next.status;
    return first.status;
  }

  @override
  String toString() {
658
    return '${objectRuntimeType(this, 'CompoundAnimation')}($first, $next)';
659 660 661 662
  }

  AnimationStatus _lastStatus;
  void _maybeNotifyStatusListeners(AnimationStatus _) {
663 664 665
    if (status != _lastStatus) {
      _lastStatus = status;
      notifyStatusListeners(status);
666 667 668 669 670
    }
  }

  T _lastValue;
  void _maybeNotifyListeners() {
671 672
    if (value != _lastValue) {
      _lastValue = value;
673 674 675 676
      notifyListeners();
    }
  }
}
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

/// An animation of [double]s that tracks the mean of two other animations.
///
/// The [status] of this animation is the status of the `right` animation if it is
/// moving, and the `left` animation otherwise.
///
/// The [value] of this animation is the [double] that represents the mean value
/// of the values of the `left` and `right` animations.
class AnimationMean extends CompoundAnimation<double> {
  /// Creates an animation that tracks the mean of two other animations.
  AnimationMean({
    Animation<double> left,
    Animation<double> right,
  }) : super(first: left, next: right);

  @override
  double get value => (first.value + next.value) / 2.0;
}
695 696 697

/// An animation that tracks the maximum of two other animations.
///
698
/// The [value] of this animation is the maximum of the values of
699 700
/// [first] and [next].
class AnimationMax<T extends num> extends CompoundAnimation<T> {
701 702 703 704
  /// Creates an [AnimationMax].
  ///
  /// Both arguments must be non-null. Either can be an [AnimationMax] itself
  /// to combine multiple animations.
705
  AnimationMax(Animation<T> first, Animation<T> next) : super(first: first, next: next);
706 707 708 709 710 711 712

  @override
  T get value => math.max(first.value, next.value);
}

/// An animation that tracks the minimum of two other animations.
///
713
/// The [value] of this animation is the maximum of the values of
714 715
/// [first] and [next].
class AnimationMin<T extends num> extends CompoundAnimation<T> {
716 717 718 719
  /// Creates an [AnimationMin].
  ///
  /// Both arguments must be non-null. Either can be an [AnimationMin] itself
  /// to combine multiple animations.
720
  AnimationMin(Animation<T> first, Animation<T> next) : super(first: first, next: next);
721 722 723

  @override
  T get value => math.min(first.value, next.value);
724
}