Search code examples
flutterdartflutter-animation

How to make animation value always run from 0 to 1


I'm trying to create an animation tween from 0 to 1:

final Tween<double> _tween = Tween(begin: 0, end: 1);

late AnimationController _controller;

late Animation<double> _animation;

And for some reason, sometimes I need to call _controller.reverse() or _controller.forward(), and my animation.value runs from 0 to 1 then from 1 to 0.

How do I always get animation.value to run from 0 to 1?


Solution

  • You can do it with _controller.forward(from: 0) or you can create a function to get animation value like this:

    double getAnimationValue(Animation animation) {
        final value = animation.value!;
    
        return animation.status == AnimationStatus.reverse ? 1 - value : value;
    }