Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do yoyo animation in flutter?

Tags:

flutter

dart

I know there is repeat() method on AnimationController but it always starts from the start.

Is there a built-in way to do one forward and one reverse animation, and to repeat that?

Thank you

like image 576
Tree Avatar asked Apr 02 '18 01:04

Tree


People also ask

How do you use tween animation in Flutter?

A linear interpolation between a beginning and ending value. Tween is useful if you want to interpolate across a range. To use a Tween object with an animation, call the Tween object's animate method and pass it the Animation object that you want to modify.


2 Answers

The repeat method supports the reverse optional named argument, so you can write

animationController.repeat(reverse: true);

This is the modern, simple solution.

like image 148
Vince Varga Avatar answered Oct 06 '22 08:10

Vince Varga


You can listen to the status of an animation using addStatusListener. And on animation end reverse it.

final AnimationController c;
...
c.addStatusListener((status) {
  if (status == AnimationStatus.completed) {
    c.reverse();
  }
  else if (status == AnimationStatus.dismissed) {
    c.forward();
  }
});
like image 31
Rémi Rousselet Avatar answered Oct 06 '22 10:10

Rémi Rousselet