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!!!
Asked
Active
Viewed 3,736 times
1 Answers
11
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.

Rémi Rousselet
- 256,336
- 79
- 519
- 432
-
My concern (or understanding) is that setState() is called by all these animations, but some animations do not have to be redrawn that often. From what I can find out: I can use each setState() to pass a parameter to indicate which animation this is, but then I would have to retain what the other animations had drawn when they had their 'epoch'. It may be (but I am not sure) that repaintBoundary can draw the screen image from the last main say, 1 second, change, and maybe draw on a small area that changes for this animation. Am I looking in the right direction? – Derek Davidson Jan 02 '20 at 18:06
-
..or maybe, even if the saving of the screen image can be done, it might also be resource consuming, and it might be just as efficient to draw everything on each animation's activation of the Build method – Derek Davidson Jan 02 '20 at 18:24
-
I don't understand what you said. Could you rephrase it? – Rémi Rousselet Jan 02 '20 at 18:34
-
I think I now understand my problem. I have an animation donig setState() every 16ms (as all animations do, and I dont think I can change that). These animations are done in a Canvas Paint. I also have Widgets, and some drawing that change every 1 second. This is what I think is inefficient, as all these things redraw the same image every 16ms. But that works, even if it is inefficient. The problem is that I have some Widgets in the Build layout that I would like to have their own animations (eg AnimatedOpacity) over times like 500ms to 2 seconds. These are redrawn every 16ms, so dont work – Derek Davidson Jan 02 '20 at 23:09