diff --git a/packages/flutter/lib/src/animation/animation_controller.dart b/packages/flutter/lib/src/animation/animation_controller.dart index b7f28aadfd..be8fa73610 100644 --- a/packages/flutter/lib/src/animation/animation_controller.dart +++ b/packages/flutter/lib/src/animation/animation_controller.dart @@ -707,13 +707,23 @@ class AnimationController extends Animation /// provided, [duration] will be used instead, which has to be set before [repeat] is /// called either in the constructor or later by using the [duration] setter. /// - /// Returns a [TickerFuture] that never completes. The [TickerFuture.orCancel] future - /// completes with an error when the animation is stopped (e.g. with [stop]). + /// If a value is passed to [count], the animation will perform that many + /// iterations before stopping. Otherwise, the animation repeats indefinitely. + /// + /// Returns a [TickerFuture] that never completes, unless a [count] is specified. + /// The [TickerFuture.orCancel] future completes with an error when the animation is + /// stopped (e.g. with [stop]). /// /// The most recently returned [TickerFuture], if any, is marked as having been /// canceled, meaning the future never completes and its [TickerFuture.orCancel] /// derivative future completes with a [TickerCanceled] error. - TickerFuture repeat({ double? min, double? max, bool reverse = false, Duration? period }) { + TickerFuture repeat({ + double? min, + double? max, + bool reverse = false, + Duration? period, + int? count, + }) { min ??= lowerBound; max ??= upperBound; period ??= duration; @@ -730,8 +740,9 @@ class AnimationController extends Animation }()); assert(max >= min); assert(max <= upperBound && min >= lowerBound); + assert(count == null || count > 0, 'Count shall be greater than zero if not null'); stop(); - return _startSimulation(_RepeatingSimulation(_value, min, max, reverse, period!, _directionSetter)); + return _startSimulation(_RepeatingSimulation(_value, min, max, reverse, period!, _directionSetter, count)); } void _directionSetter(_AnimationDirection direction) { @@ -959,8 +970,19 @@ class _InterpolationSimulation extends Simulation { typedef _DirectionSetter = void Function(_AnimationDirection direction); class _RepeatingSimulation extends Simulation { - _RepeatingSimulation(double initialValue, this.min, this.max, this.reverse, Duration period, this.directionSetter) - : _periodInSeconds = period.inMicroseconds / Duration.microsecondsPerSecond, + _RepeatingSimulation( + double initialValue, + this.min, + this.max, + this.reverse, + Duration period, + this.directionSetter, + this.count, + ) : assert( + count == null || count > 0, + 'Count shall be greater than zero if not null', + ), + _periodInSeconds = period.inMicroseconds / Duration.microsecondsPerSecond, _initialT = (max == min) ? 0.0 : ((clampDouble(initialValue, min, max) - min) / (max - min)) * (period.inMicroseconds / Duration.microsecondsPerSecond) { assert(_periodInSeconds > 0.0); assert(_initialT >= 0.0); @@ -969,11 +991,14 @@ class _RepeatingSimulation extends Simulation { final double min; final double max; final bool reverse; + final int? count; final _DirectionSetter directionSetter; final double _periodInSeconds; final double _initialT; + late final double _exitTimeInSeconds = (count! * _periodInSeconds) - _initialT; + @override double x(double timeInSeconds) { assert(timeInSeconds >= 0.0); @@ -995,5 +1020,9 @@ class _RepeatingSimulation extends Simulation { double dx(double timeInSeconds) => (max - min) / _periodInSeconds; @override - bool isDone(double timeInSeconds) => false; + bool isDone(double timeInSeconds) { + // if [timeInSeconds] elapsed the [_exitTimeInSeconds] && [count] is not null, + // consider marking the simulation as "DONE" + return count != null && (timeInSeconds >= _exitTimeInSeconds); + } } diff --git a/packages/flutter/test/animation/animation_controller_test.dart b/packages/flutter/test/animation/animation_controller_test.dart index 76dffa95b5..5a4e9b0821 100644 --- a/packages/flutter/test/animation/animation_controller_test.dart +++ b/packages/flutter/test/animation/animation_controller_test.dart @@ -1227,6 +1227,107 @@ void main() { ); }); + group('count tests for repeated animation', () { + test( + 'calling repeat by setting count as zero shall throw Assertion', + () { + final AnimationController controller = AnimationController( + duration: const Duration(milliseconds: 100), + value: 0.0, + vsync: const TestVSync(), + ); + + expect(controller.value, 0.0); + expect( + () => controller.repeat(reverse: true, count: 0), + throwsAssertionError, + ); + }, + ); + + test( + 'calling repeat by setting count as negative shall throw Assertion', + () { + final AnimationController controller = AnimationController( + duration: const Duration(milliseconds: 100), + value: 0.0, + vsync: const TestVSync(), + ); + + expect(controller.value, 0.0); + expect( + () => controller.repeat(reverse: true, count: -1), + throwsAssertionError, + ); + }, + ); + + test( + 'calling repeat by setting count as valid with reverse as false, shall run animation accordingly', + () { + final AnimationController controller = AnimationController( + duration: const Duration(milliseconds: 100), + value: 0.0, + vsync: const TestVSync(), + ); + + expect(controller.value, 0.0); + controller.repeat(count: 1); + tick(Duration.zero); + tick(const Duration(milliseconds: 25)); + expect(controller.value, 0.25); + tick(const Duration(milliseconds: 50)); + expect(controller.value, 0.5); + tick(const Duration(milliseconds: 99)); + expect(controller.value, 0.99); + tick(const Duration(milliseconds: 100)); + expect(controller.value, 0); + + controller.reset(); + + expect(controller.value, 0.0); + controller.repeat(count: 2); + tick(Duration.zero); + tick(const Duration(milliseconds: 25)); + expect(controller.value, 0.25); + tick(const Duration(milliseconds: 50)); + expect(controller.value, 0.5); + tick(const Duration(milliseconds: 200)); + expect(controller.value, 0); + + controller.reset(); + controller.dispose(); + }, + ); + + test( + 'calling repeat by setting count as valid with reverse as true, shall run animation accordingly', + () { + final AnimationController controller = AnimationController( + duration: const Duration(milliseconds: 100), + value: 0.0, + vsync: const TestVSync(), + ); + + expect(controller.value, 0.0); + controller.repeat(reverse: true, count: 4); + tick(Duration.zero); + tick(const Duration(milliseconds: 25)); + expect(controller.value, 0.25); + tick(const Duration(milliseconds: 50)); + expect(controller.value, 0.5); + tick(const Duration(milliseconds: 99)); + expect(controller.value, 0.99); + tick(const Duration(milliseconds: 100)); + expect(controller.value, 1); + tick(const Duration(milliseconds: 60)); + expect(double.parse(controller.value.toStringAsFixed(1)), 0.6); + + controller.reset(); + controller.dispose(); + }, + ); + }); } class TestSimulation extends Simulation {