• Simon Friis Vindum's avatar
    Fix initialization of time in repeat on AnimationController (#142887) · 6c78e36c
    Simon Friis Vindum authored
    This PR fixes #142885.
    
    The issue is that in `_RepeatingSimulation` the initial time is calculated as follows:
    
    ```
    (initialValue / (max - min)) * (period.inMicroseconds / Duration.microsecondsPerSecond)
    ```
    
    This calculation does not work in general. For instance, if `max` is 300, `min` is 100, and `initialValue` is 100 then `initialValue / (max - min)` is 1/2 when it should be 0
    
    The current tests work by happenstance because the numbers used happen to work. To reveal the bug I've added some more tests similar to the existing ones but with different numbers.
    
    A "side-effect" of the incorrect calculation is that if `initialValue` is 0, then the animation will always start from `min` no matter what. For instance, in one of the tests, an `AnimationController` with the value 0 is told to `repeat` between 0.5 and 1.0, and this starts the animation from 0.5. To preserve this behavior, and to more generally handle the case where the initial value is out of bounds, this PR clamps the initial value to be within the lower and upper bounds of the repetition.
    
    Just for reference, this calculation was introduced at https://github.com/flutter/flutter/pull/25125.
    6c78e36c
animation_controller.dart 37.1 KB