Home > OS >  Difference between TickerProviderStateMixin and SingleTickerProviderStateMixin?
Difference between TickerProviderStateMixin and SingleTickerProviderStateMixin?

Time:09-27

When I work with AnimationController it requires a vsync parameter. I have researched, should I use TickerProviderStateMixin or SingleTickerProviderStateMixin, currently I still do not know what is the difference between them. Thanks if you give me an explanation about it. References: https://api.flutter.dev/flutter/widgets/SingleTickerProviderStateMixin-mixin.html https://api.flutter.dev/flutter/widgets/TickerProviderStateMixin-mixin.html

CodePudding user response:

When you are using single AnimationController use SingleTickerProviderStateMixin.

If you need more than one AnimationController use TickerProviderStateMixin.

We need TickerProviderStateMixin below snippet because we have two AnimationController

class _TrickerExampleState extends State<TrickerExample>
    with TickerProviderStateMixin {
  late AnimationController controller1;
  late Animation<double> fadeAnimation;

  late AnimationController controller2;
  late Animation<Alignment> positionAnimation;

CodePudding user response:

Remember the main propose of the Ticker: Calls its callback once per animation frame. That's why the AnimationController needs a Ticker and that's why depending on the number of AnimationControllers you need one Mixin is more efficient like they said. So, if you can create all your animations with one AnimationController then use the SingleTickerProviderStateMixin. If you need more than one AnimationController then use the TickerProviderStateMixin.

CodePudding user response:

as the documentation says, they will provide Ticker.

The difference is that SingleTickerProviderStateMixin Provides one Ticker and has less MixinApplication than TickerProviderStateMixin


I think we have to know what is Ticker. here a snippet explanation : Using Flutter’s animation framework, this is achieved through a Ticker TickerProvider.

Ticker is the refresh rate of our animations. This is what we want to pause when our clock is hidden.

TickerProvider, usually implemented with SingleTickerProviderStateMixin, is a binding between Ticker and external factors.

here the complete article about it : https://dash-overflow.net/articles/why_vsync/

  • Related