Flutter 动画图标 AnimationController
Flutter animated icon AnimationController
所以我有两个页面:第一页是我的主页,从那里我可以通过一个按钮访问我的第二页。第二页是我在里面放了一个动画图标的地方。
我正在尝试为图标制作动画,到目前为止一切正常。图标像我想要的那样弹跳,但是当我回到我的主页时突然出现一个错误并且该应用程序不再运行(我添加了一个屏幕截图但我真的不明白是什么我需要更改我的代码内部)。我能做些什么来防止这种情况发生?
这是我的代码:
class InfoScreen extends StatefulWidget {
@override
_InfoScreen createState() => _InfoScreen();
}
@override
class _InfoScreen extends State<InfoScreen> with TickerProviderStateMixin {
AnimationController _animationController;
Animation<double> _animation;
bool _isPlaying = false;
void _animate() {
if (_isPlaying)
_animationController.stop();
else
_animationController.forward();
setState(() {
_isPlaying = !_isPlaying;
});
}
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 450),
);
_animation = Tween<double>(begin: 0.85, end: 1.05).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeIn));
_animationController.forward();
_animation.addStatusListener((status) {
if (status == AnimationStatus.completed)
_animationController.reverse();
else if (status == AnimationStatus.dismissed)
_animationController.forward();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Info'),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xffFBD23E), Color(0xffF6BE03)],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
),
),
),
body: Container(
constraints: BoxConstraints.expand(),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xffFEFDFD), Color(0xffBDBDB2)],
begin: Alignment.topLeft,
end: Alignment.bottomRight),
),
child: ScaleTransition(
scale: _animation,
child: Icon(
Icons.favorite,
color: Color(0xffF40930),
),
),
],
),
),
);
}
}
我们需要处理 AnimationController
。覆盖处理方法并处理 _animationController
.
@override
void dispose() {
_animationController.dispose();
super.dispose();
}
所以我有两个页面:第一页是我的主页,从那里我可以通过一个按钮访问我的第二页。第二页是我在里面放了一个动画图标的地方。 我正在尝试为图标制作动画,到目前为止一切正常。图标像我想要的那样弹跳,但是当我回到我的主页时突然出现一个错误并且该应用程序不再运行(我添加了一个屏幕截图但我真的不明白是什么我需要更改我的代码内部)。我能做些什么来防止这种情况发生?
这是我的代码:
class InfoScreen extends StatefulWidget {
@override
_InfoScreen createState() => _InfoScreen();
}
@override
class _InfoScreen extends State<InfoScreen> with TickerProviderStateMixin {
AnimationController _animationController;
Animation<double> _animation;
bool _isPlaying = false;
void _animate() {
if (_isPlaying)
_animationController.stop();
else
_animationController.forward();
setState(() {
_isPlaying = !_isPlaying;
});
}
@override
void initState() {
super.initState();
_animationController = AnimationController(
vsync: this,
duration: Duration(milliseconds: 450),
);
_animation = Tween<double>(begin: 0.85, end: 1.05).animate(
CurvedAnimation(parent: _animationController, curve: Curves.easeIn));
_animationController.forward();
_animation.addStatusListener((status) {
if (status == AnimationStatus.completed)
_animationController.reverse();
else if (status == AnimationStatus.dismissed)
_animationController.forward();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Info'),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xffFBD23E), Color(0xffF6BE03)],
begin: Alignment.topCenter,
end: Alignment.bottomCenter),
),
),
),
body: Container(
constraints: BoxConstraints.expand(),
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xffFEFDFD), Color(0xffBDBDB2)],
begin: Alignment.topLeft,
end: Alignment.bottomRight),
),
child: ScaleTransition(
scale: _animation,
child: Icon(
Icons.favorite,
color: Color(0xffF40930),
),
),
],
),
),
);
}
}
我们需要处理 AnimationController
。覆盖处理方法并处理 _animationController
.
@override
void dispose() {
_animationController.dispose();
super.dispose();
}