I use Provider to manage state in my Flutter demo.
I want to animate my widget but it seems that AnimateController needs a sync parameter that comes from the state of a stateful widget.
As far as I know, Provider is not recommended to use with a stateful widget.
Can I manage the AnimationController with Provider?
Or must use both Provider and stateful widget?
A linear interpolation between a beginning and ending value. Tween is useful if you want to interpolate across a range. To use a Tween object with an animation, call the Tween object's animate method and pass it the Animation object that you want to modify.
If you want to create AnimationController in the StatelessWidget you must pass TickerProviderStateMixin in the Constructor of your StatelessWidget, look at my sample code :
import 'package:flutter/material.dart';
main() {
runApp(TestyApp());
}
class TestyApp extends StatefulWidget {
@override
_TestyAppState createState() => _TestyAppState();
}
class _TestyAppState extends State<TestyApp> with TickerProviderStateMixin {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: TestAnimationInStatelessWidget(
tickerProviderStateMixin: this,
),
);
}
}
class TestAnimationInStatelessWidget extends StatelessWidget {
final TickerProviderStateMixin tickerProviderStateMixin;
const TestAnimationInStatelessWidget(
{Key key, @required this.tickerProviderStateMixin})
: assert(tickerProviderStateMixin != null),
super(key: key);
@override
Widget build(BuildContext context) {
var _animationController = AnimationController(
vsync: tickerProviderStateMixin, duration: Duration(seconds: 1));
var _animation = Tween<double>(begin: 0, end: 2).animate(CurvedAnimation(
parent: _animationController, curve: Curves.easeInOut));
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
RotationTransition(
turns: _animation,
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.blueGrey,
borderRadius: BorderRadius.all(Radius.circular(15)),
),
),
),
SizedBox(
height: 30,
),
RaisedButton(
onPressed: () {
_animationController.reset();
_animationController.forward();
},
child: Text("Start Animation"),
)
],
),
),
);
}
}
Can I manage the AnimationController with Provider?
Or must use both Provider and stateful widget?
I don't know if this is the best way to do it, but here is how I do this :
I use didUpdateWidget in a Stateful widget. I pass the value from the provider to the parent and trigger the animation in the didUpdateWidget
method defined in the child. didUpdateWidget
being triggered itself when I notify listeners from a change in the Provider.
class MyWidget extends StatefulWidget {
final String valueFromProvider;
MyWidget({this.valueFromProvider});
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> with TickerProviderStateMixin {
@override
void didUpdateWidget(MyWidget oldWidget) {
// TODO: implement didUpdateWidget
super.didUpdateWidget(oldWidget);
if (oldWidget.valueFromProvider == "whatever you want" &&
widget.valueFromProvider == "what you want that changed") {
// trigger animations methods here
}
}
// here all the animations methods,dispose method, etc.
@override
Widget build(BuildContext context) {
return Container();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With