diff --git a/packages/flutter/lib/src/animation/animations.dart b/packages/flutter/lib/src/animation/animations.dart index 1f0047386e..def1cc3f3c 100644 --- a/packages/flutter/lib/src/animation/animations.dart +++ b/packages/flutter/lib/src/animation/animations.dart @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:math' as math; import 'dart:ui' show VoidCallback; import 'package:flutter/foundation.dart'; @@ -544,6 +545,10 @@ class TrainHoppingAnimation extends Animation /// /// 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;` +/// +/// By default, the [status] of a [CompoundAnimation] is the status of the +/// [next] animation if [next] is moving, and the status of the [first] +/// animation otherwise. abstract class CompoundAnimation extends Animation with AnimationLazyListenerMixin, AnimationLocalListenersMixin, AnimationLocalStatusListenersMixin { /// Creates a CompoundAnimation. Both arguments must be non-null. Either can @@ -577,10 +582,12 @@ abstract class CompoundAnimation extends Animation next.removeStatusListener(_maybeNotifyStatusListeners); } + /// Gets the status of this animation based on the [first] and [next] status. + /// + /// The default is that if the [next] animation is moving, use its status. + /// Otherwise, default to [first]. @override AnimationStatus get status { - // If one of the sub-animations is moving, use that status. Otherwise, - // default to `first`. if (next.status == AnimationStatus.forward || next.status == AnimationStatus.reverse) return next.status; return first.status; @@ -625,3 +632,25 @@ class AnimationMean extends CompoundAnimation { @override double get value => (first.value + next.value) / 2.0; } + +/// An animation that tracks the maximum of two other animations. +/// +/// The [value] of this animation is the maximum of the values of +/// [first] and [next]. +class AnimationMax extends CompoundAnimation { + AnimationMax(Animation first, Animation next): super(first: first, next: next); + + @override + T get value => math.max(first.value, next.value); +} + +/// An animation that tracks the minimum of two other animations. +/// +/// The [value] of this animation is the maximum of the values of +/// [first] and [next]. +class AnimationMin extends CompoundAnimation { + AnimationMin(Animation first, Animation next): super(first: first, next: next); + + @override + T get value => math.min(first.value, next.value); +} \ No newline at end of file diff --git a/packages/flutter/test/animation/animations_test.dart b/packages/flutter/test/animation/animations_test.dart index f72dccd21b..c9f2ecabe8 100644 --- a/packages/flutter/test/animation/animations_test.dart +++ b/packages/flutter/test/animation/animations_test.dart @@ -153,6 +153,76 @@ void main() { expect(log, isEmpty); }); + test('AnimationMax control test', () { + final AnimationController first = new AnimationController( + value: 0.5, + vsync: const TestVSync(), + ); + final AnimationController second = new AnimationController( + vsync: const TestVSync(), + ); + + final AnimationMax max = new AnimationMax(first, second); + + expect(max, hasOneLineDescription); + expect(max.value, equals(0.5)); + + final List log = []; + void logValue() { + log.add(max.value); + } + + max.addListener(logValue); + + second.value = 1.0; + + expect(max.value, equals(1.0)); + expect(log, equals([1.0])); + log.clear(); + + max.removeListener(logValue); + + first.value = 0.0; + + expect(max.value, equals(1.0)); + expect(log, isEmpty); + }); + + test('AnimationMin control test', () { + final AnimationController first = new AnimationController( + value: 0.5, + vsync: const TestVSync(), + ); + final AnimationController second = new AnimationController( + vsync: const TestVSync(), + ); + + final AnimationMin min = new AnimationMin(first, second); + + expect(min, hasOneLineDescription); + expect(min.value, equals(0.0)); + + final List log = []; + void logValue() { + log.add(min.value); + } + + min.addListener(logValue); + + second.value = 1.0; + + expect(min.value, equals(0.5)); + expect(log, equals([0.5])); + log.clear(); + + min.removeListener(logValue); + + first.value = 0.25; + + expect(min.value, equals(0.25)); + expect(log, isEmpty); + }); + test('CurvedAnimation with bogus curve', () { final AnimationController controller = new AnimationController( vsync: const TestVSync(),