[go: nahoru, domu]

Skip to content

Commit

Permalink
Fix initialization of time in repeat on AnimationController
Browse files Browse the repository at this point in the history
  • Loading branch information
paldepind committed Feb 20, 2024
1 parent a2c7ed9 commit 115261c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ 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,
_initialT = (max == min) ? 0.0 : (initialValue / (max - min)) * (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);
}
Expand Down
45 changes: 43 additions & 2 deletions packages/flutter/test/animation/animation_controller_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,8 @@ void main() {
);

test(
'calling repeat with specified min and max values makes the animation '
'alternate between min and max values on each repeat',
'calling repeat with specified min and max values between 0 and 1 makes '
'the animation alternate between min and max values on each repeat',
() {
final AnimationController controller = AnimationController(
duration: const Duration(milliseconds: 100),
Expand Down Expand Up @@ -799,6 +799,47 @@ void main() {
tick(Duration.zero);
tick(const Duration(milliseconds: 125));
expect(controller.value, 1.0);

controller.reset();
controller.value = 0.2;
expect(controller.value, 0.2);

controller.repeat(reverse: true, min: 0.2, max: 0.6);
tick(Duration.zero);
tick(const Duration(milliseconds: 50));
expect(controller.value, 0.4);
controller.dispose();
},
);

test(
'calling repeat with negative min value and positive max value makes the '
'animation alternate between min and max values on each repeat',
() {
final AnimationController controller = AnimationController(
duration: const Duration(milliseconds: 100),
value: 1.0,
lowerBound: -1,
upperBound: 3,
vsync: const TestVSync(),
);

expect(controller.value, 1.0);

controller.repeat(min: 1, max: 3);
tick(Duration.zero);
expect(controller.value, 1);
tick(const Duration(milliseconds: 50));
expect(controller.value, 2);

controller.reset();
controller.value = 0.0;

controller.repeat(min: -1, max: 3);
tick(Duration.zero);
expect(controller.value, 0);
tick(const Duration(milliseconds: 25));
expect(controller.value, 1);
controller.dispose();
},
);
Expand Down

0 comments on commit 115261c

Please sign in to comment.