Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter difference between Timer and AnimationController uses

I have animations working and a Timer. I understand that the Timer is the best way to get the Second Ticks ticking at exactly the second mark. For Animations I have used both AnimationController and the Timer with a small duration time (100ms). I think I must be missing something in that I don't completely understand when a Timer and AnimationController should each be used. They both cause a setState() and thus a Build(and thus redraw). I suppose one idea is if I could retain all the screen, and just redraw a small change in an animation. full disclosure: I am working on the Flutter Clock Challenge!!!

like image 230
Derek Davidson Avatar asked Mar 20 '26 06:03

Derek Davidson


1 Answers

Timer is unrelated to Flutter, and is just a timer like you'd fine in any other language.

On the other hand, AnimationController (and Ticker, its equivalent of Timer) is Flutter specific.

The difference with a Timer is that, by using AnimationController, the "ticker" can be muted, slowed, or mocked.

As such, using AnimationController we have a few benefits:

  • if the associated widget isn't visible, the animation is paused (that's what that vsync arg is used for)
  • we get access to a "slow animations" devtool which makes the seconds slower for animations only
  • tests can "mock" these timers. As such, if an animation last 3 seconds, a test can simulate the entire animation without having to actually wait for 3 seconds.
like image 70
Rémi Rousselet Avatar answered Mar 21 '26 19:03

Rémi Rousselet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!