Is there a way to create animationController inside a stateless widget? Presently I am unable to pass vsync:this inside controller constructor...
Asked
Active
Viewed 9,315 times
1 Answers
4
No you can't because StatelessWidget
doesn't implement State
which SingleTickerProvider
mixin
is defined to work only on classes that implement it
If you are looking for a clean solution see flutter hooks they let you use an animation contoller without the boilerplate needed if you use StatefulWidget

HII
- 3,420
- 1
- 14
- 35
-
2you can use any `State`that mixes with `TickerProviderStateMixin` - for example `Scaffold.of()` method gives you `ScaffoldState` which you can use as a `vsync` parameter – pskink Apr 19 '20 at 03:49
-
@pskink but this does not discard my answer right?you can't use the stateless widget directly with the mixin also in this case if you define animation controllers how will you dispose them? – HII Apr 19 '20 at 06:52
-
This tutorial from flutter.dev says " Dispose of the controller when the State object is discarded to prevent memory leaks." https://flutter.dev/docs/development/ui/animations/tutorial – HII Apr 19 '20 at 07:17
-
1I found a workaround, I passed the Ticker object from parent (which is stateful) to child (which is stateless), so now I am able to use animationController inside stateless widget – PiyushK Apr 19 '20 at 08:48
-
@PiyushK and how do you dispose the controller? – HII Jul 17 '22 at 10:30