Taio
Taio

Reputation: 3734

How can I run this setState function for say every 1 sec

void play(){    
setState(() {
  tween = new VibesTween(
    tween.evaluate(animation),
    new Wave.random(size, random),
  );
  animation.forward(from: 0.0);
});
}

I need to call this play function which animates a particular animation. The only problem is it runs every time I click a button. I need it to call state every 1 second and rerender the animation again for every second. How can I achieve this?

Upvotes: 2

Views: 1304

Answers (2)

Naveen
Naveen

Reputation: 1523

You may call the play() method every one second by passing it as a Callback function parameter in Timer.Periodic(Duration, Callback) method.

const oneSec = const Duration(seconds: 1);
new Timer.periodic(oneSec, (Timer t) -> play());

Upvotes: 1

Edman
Edman

Reputation: 5605

You don't really need to call setState every second, you just need a repeating animation. And AnimationController has a repeat method for cases like this.

To set it off in its most basic form you can just call,

animationController.repeat();

But you can also provide overrides, as in

animationController.repeat(min: 0.0, max: 1.0, period: Duration(seconds: 1));

Upvotes: 3

Related Questions