如何在Flutter中为RaisedButton的颜色设置动画? [英] How to animate the color of a RaisedButton in Flutter?
本文介绍了如何在Flutter中为RaisedButton的颜色设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个RaisedButton
.我想每次用户单击时,将其颜色从绿色变为红色,反之亦然.
I have a RaisedButton
. I want to animate it's color from green to red, and vice versa, every time the user clicks it.
这怎么办?
推荐答案
class ChangeRaisedButtonColor extends StatefulWidget {
@override
ChangeRaisedButtonColorState createState() => ChangeRaisedButtonColorState();
}
class ChangeRaisedButtonColorState extends State<ChangeRaisedButtonColor>
with SingleTickerProviderStateMixin {
AnimationController _animationController;
Animation _colorTween;
@override
void initState() {
_animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 300));
_colorTween = ColorTween(begin: Colors.red, end: Colors.green)
.animate(_animationController);
super.initState();
}
@override
void dispose() {
super.dispose();
_animationController.dispose();
}
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: _colorTween,
builder: (context, child) => RaisedButton(
child: Text("Change my color"),
color: _colorTween.value,
onPressed: () {
if (_animationController.status == AnimationStatus.completed) {
_animationController.reverse();
} else {
_animationController.forward();
}
},
),
);
}
}
这篇关于如何在Flutter中为RaisedButton的颜色设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文