State要加上with SingleTickerProviderStateMixin,
Tween(begin: 0.0, end: 90.0)是在Duration(milliseconds: 100)一百毫秒里返回0.0-90.0的double值,需要调用的地方用lineAnimation.value取值,..addListener,这个前面加..的语法是指用Tween(begin: 0.0, end: 90.0).animate(lineController)的返回值来调用addListener
class _FollowListState extends State<FollowList>
with SingleTickerProviderStateMixin {
AnimationController lineController;
Animation<double> lineAnimation;
@override
initState() {
super.initState();
lineController = AnimationController(
vsync: this, duration: const Duration(milliseconds: 100));
lineAnimation =
Tween(begin: 0.0, end: 90.0)
.animate(lineController)
..addListener(() {
setState(() {});
});
}
@override
dispose() {
// 释放
lineController.dispose();
super.dispose();
}
调用:
Container(
margin: EdgeInsets.only(left: lineAnimation.value),
width: 90,
height: 3,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(1.5),
color: mainColor,
),
),
执行动画:
lineController.forward();
反向执行:
lineController.reverse();
加入addStatusListener可以监听动画执行状态,可以在回调里进行动画循环:
lineAnimation = Tween(begin: 0.0, end: 90.0).animate(lineController)
..addListener(() {
setState(() {});
})
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
lineController.reverse();
} else if (status == AnimationStatus.dismissed) {
lineController.forward();
}
});
网友评论