SingleTickerProviderStateMixin Vs TickerProviderStateMixin

Ranveer Singh Gour
2 min readApr 28, 2023

--

In Flutter, `SingleTickerProviderStateMixin` and `TickerProviderStateMixin` are two mixins that can be used to provide the `vsync` parameter for animations using `AnimationController` in a `StatefulWidget` or a `State` object.

The main difference between `SingleTickerProviderStateMixin` and `TickerProviderStateMixin` is in how they handle multiple `AnimationController` instances within the same widget tree:

1. `SingleTickerProviderStateMixin`: This mixin allows a widget to act as a `TickerProvider` for a single `AnimationController`. It ensures that only one `Ticker` is created and managed for a single `AnimationController`, even if multiple `AnimationController` instances are used within the same widget tree. This mixin is suitable when you have only one `AnimationController` in your widget tree.

Example usage of `SingleTickerProviderStateMixin`:

class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> with SingleTickerProviderStateMixin {
AnimationController _controller;
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this);
// … other initialization
}
// … rest of the widget code
}

2. `TickerProviderStateMixin`: This mixin allows a widget to act as a `TickerProvider` for multiple `AnimationController` instances within the same widget tree. It creates a separate `Ticker` for each `AnimationController` instance used, which can lead to better performance when you have multiple `AnimationController` instances in your widget tree.

Example usage of `TickerProviderStateMixin`:

class MyWidget extends StatefulWidget {
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> with TickerProviderStateMixin {
AnimationController _controller1;
AnimationController _controller2;
@override
void initState() {
super.initState();
_controller1 = AnimationController(vsync: this);
_controller2 = AnimationController(vsync: this);
// … other initialization
}
// … rest of the widget code
}

In summary, `SingleTickerProviderStateMixin` is used when you have only one `AnimationController` in your widget tree, and `TickerProviderStateMixin` is used when you have multiple `AnimationController` instances in your widget tree and need separate `Ticker` instances for each one.

Now You Will ask What is Ticker here ?

In Flutter, a `Ticker` is an object that provides a callback mechanism that fires on every frame of an animation. It is used to drive animations and synchronize them with the frame rate of the app. `Ticker` is typically used in combination with `AnimationController` to create and manage animations.

`AnimationController` is a Flutter class that allows you to create animations with values that change over time. It has a `vsync` parameter that requires a `TickerProvider` object, which is responsible for providing the `Ticker` that drives the animation. The `vsync` parameter ensures that the animation updates are synchronized with the frame rate of the app, preventing unnecessary updates and optimizing performance.

In the context of `SingleTickerProviderStateMixin` and `TickerProviderStateMixin`, the `Ticker` is automatically created and managed by the mixin, and it is used to drive the animations created with `AnimationController`. The `vsync` parameter of `AnimationController` is set to the mixin, which provides the necessary `Ticker` for the animations to update on each frame.

Using `Ticker` and `AnimationController` in combination with `vsync` parameter and appropriate mixins, you can create smooth and performant animations in Flutter.

--

--

Ranveer Singh Gour
Ranveer Singh Gour

Written by Ranveer Singh Gour

Senior Software Engineer with 4+ years in Flutter development. Passionate about scalable apps, clean architecture, and sharing tech insights. #Flutter #KMM #UI

No responses yet