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
import 'dart:math' as math;
7

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

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

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

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

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

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

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

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

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

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

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

42 43 44 45 46
/// 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.
47
const Animation<double> kAlwaysCompleteAnimation = _AlwaysCompleteAnimation();
48

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

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

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

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

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

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

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

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

74 75 76 77 78
/// 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.
79
const Animation<double> kAlwaysDismissedAnimation = _AlwaysDismissedAnimation();
80

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

94
  @override
95
  final T value;
96

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

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

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

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

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

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

118 119 120 121 122 123 124 125
/// 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.
126
mixin AnimationWithParentMixin<T> {
127 128 129
  /// The animation whose value this animation will proxy.
  ///
  /// This animation must remain the same for the lifetime of this object. If
130
  /// you wish to proxy a different animation at different times, consider using
131 132
  /// [ProxyAnimation].
  Animation<T> get parent;
133

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

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

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

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

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

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

160 161 162 163
/// 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
164
/// object, and then later change the animation from which the proxy receives
165
/// its value.
166
class ProxyAnimation extends Animation<double>
167
  with AnimationLazyListenerMixin, AnimationLocalListenersMixin, AnimationLocalStatusListenersMixin {
168 169 170 171 172

  /// 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.
173
  ProxyAnimation([Animation<double>? animation]) {
174 175
    _parent = animation;
    if (_parent == null) {
176
      _status = AnimationStatus.dismissed;
177 178 179 180
      _value = 0.0;
    }
  }

181 182
  AnimationStatus? _status;
  double? _value;
183

184 185 186 187
  /// 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.
188 189 190
  Animation<double>? get parent => _parent;
  Animation<double>? _parent;
  set parent(Animation<double>? value) {
191
    if (value == _parent)
192
      return;
193
    if (_parent != null) {
194 195
      _status = _parent!.status;
      _value = _parent!.value;
196 197 198
      if (isListening)
        didStopListening();
    }
199 200
    _parent = value;
    if (_parent != null) {
201 202
      if (isListening)
        didStartListening();
203
      if (_value != _parent!.value)
204
        notifyListeners();
205 206
      if (_status != _parent!.status)
        notifyStatusListeners(_parent!.status);
207 208 209 210 211
      _status = null;
      _value = null;
    }
  }

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

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

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

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

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

242 243 244
/// An animation that is the reverse of another animation.
///
/// If the parent animation is running forward from 0.0 to 1.0, this animation
245 246 247 248 249
/// 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.
250 251 252 253 254 255 256
///
/// 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.
257
class ReverseAnimation extends Animation<double>
258
  with AnimationLazyListenerMixin, AnimationLocalStatusListenersMixin {
259 260 261 262

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

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

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

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

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

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

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

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

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

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

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

317 318
/// An animation that applies a curve to another animation.
///
319
/// [CurvedAnimation] is useful when you want to apply a non-linear [Curve] to
320 321
/// an animation object, especially if you want different curves when the
/// animation is going forward vs when it is going backward.
322
///
323 324 325 326
/// 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.
327 328
///
/// If you want to apply a [Curve] to a [Tween], consider using [CurveTween].
329
///
330
/// {@tool snippet}
331 332 333 334 335 336 337 338 339 340
///
/// 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,
/// );
/// ```
341
/// {@end-tool}
342
/// {@tool snippet}
343 344 345 346 347 348 349 350 351 352 353 354 355
///
/// 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,
/// );
/// ```
356
/// {@end-tool}
357 358 359 360 361 362 363 364 365 366 367
///
/// 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].
368
class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<double> {
369 370 371
  /// Creates a curved animation.
  ///
  /// The parent and curve arguments must not be null.
372
  CurvedAnimation({
373 374
    required this.parent,
    required this.curve,
375
    this.reverseCurve,
376 377
  }) : assert(parent != null),
       assert(curve != null) {
378 379
    _updateCurveDirection(parent.status);
    parent.addStatusListener(_updateCurveDirection);
380 381
  }

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

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

  /// The curve to use in the reverse direction.
  ///
391 392 393 394 395 396 397 398 399 400
  /// 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.
  ///
401
  /// If this field is null, uses [curve] in both directions.
402
  Curve? reverseCurve;
403 404 405 406 407

  /// 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
408
  /// animation is used to animate.
409
  AnimationStatus? _curveDirection;
410

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

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

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

434
    final double t = parent.value;
435 436 437
    if (activeCurve == null)
      return t;
    if (t == 0.0 || t == 1.0) {
438 439 440 441
      assert(() {
        final double transformedValue = activeCurve.transform(t);
        final double roundedTransformedValue = transformedValue.round().toDouble();
        if (roundedTransformedValue != t) {
442 443 444 445 446 447
          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.'
          );
448 449
        }
        return true;
450
      }());
451 452 453 454
      return t;
    }
    return activeCurve.transform(t);
  }
455 456 457 458 459

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

466 467
enum _TrainHoppingMode { minimize, maximize }

468 469
/// 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
470
/// going in the opposite direction, or because the one overtakes the other),
471 472 473 474 475 476 477 478 479 480
/// 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.
481 482 483 484 485
///
/// 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.
486
class TrainHoppingAnimation extends Animation<double>
487
  with AnimationEagerListenerMixin, AnimationLocalListenersMixin, AnimationLocalStatusListenersMixin {
488 489 490 491

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

516 517 518 519
  /// 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.
520 521 522 523
  Animation<double>? get currentTrain => _currentTrain;
  Animation<double>? _currentTrain;
  Animation<double>? _nextTrain;
  _TrainHoppingMode? _mode;
524

525 526 527 528 529
  /// 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.
530
  VoidCallback? onSwitchedTrain;
531

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

542
  @override
543
  AnimationStatus get status => _currentTrain!.status;
544

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

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

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

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

/// 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;`
609 610 611
///
/// By default, the [status] of a [CompoundAnimation] is the status of the
/// [next] animation if [next] is moving, and the status of the [first]
612
/// animation otherwise.
613 614 615 616 617
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({
618 619
    required this.first,
    required this.next,
620 621
  }) : assert(first != null),
       assert(next != null);
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

  /// 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);
  }

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

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

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

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

/// 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({
689 690
    required Animation<double> left,
    required Animation<double> right,
691 692 693 694 695
  }) : super(first: left, next: right);

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

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

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

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

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