Reputation: 30879
In Flutter, some animation classes need you to pass vsync
in the constructor, while others don't. For example, AnimatedSize
needs vsync. AnimatedContainer
doesn't.
1) Why some animation classes need it and some don't?
2) Does that relate to those classes having state (their own internal vsync)?
3) All classes that use vsync could be turned into classes that don't? And what's the trade-off?
Upvotes: 2
Views: 1979
Reputation: 34210
Flutter good animation framework has
vsync takes a TickerProvider as an argument, that's why we use SingleTickerProviderStateMixin and as the named describes TickerProvider provides Ticker which simply means it tells our app about the Frame update(or Screen Update), so that our AnimationController can generate a new value and we can redraw the animated widget.
TickerProvider, usually implemented with SingleTickerProviderStateMixin
, is binding between Ticker and external factors.
The main advantage of having vsync(TickerProvider)
is used with Controllers which can be paused when your widgets are not on screen. If we don't have this then rendering will perform continuously even if the screen is not visible to user.
controller = AnimationController(
duration: Duration(),
vsync: this,
);
Upvotes: 1
Reputation: 7148
vsync is the property which represents the TickerProvider (i.e., Tick is similar to clock's tick which means that at every certain duration TickerProvider will render the class state and redraw the object.)
vsync property is required only on that constructors which requires to render it's class state at every certain off-set time when we need to render our components or widgets to redraw and reflect the UI.
vsync can be used with the classes which requires certain transition or animation to re-render to draw different objects.
For Ex : vsync with AnimationController() class will inform our app to redraw the frames at every fraction of seconds to generate the animation for providing greater user experience.
Upvotes: 3