Home > Enterprise >  How to make animation value always run from 0 to 1
How to make animation value always run from 0 to 1

Time:11-23

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?

CodePudding user response:

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;
}

CodePudding user response:

You can use .repeat().

_controller.repeat();

If you like to reverse while repeat

_controller.repeat(reverse: true);

Also, just to get on initial state, you can use

_controller.reset();

then can be start

_controller.forward();
  • Related