【发布时间】:2020-10-30 03:23:57
【问题描述】:
最少的可重现代码:
static const Duration _duration = Duration(seconds: 5);
static const Curve _curve = Curves.fastOutSlowIn;
AnimationController _controller;
bool _flag = false;
@override
void initState() {
super.initState();
_controller = AnimationController(vsync: this, duration: _duration);
}
@override
Widget build(BuildContext context) {
final box = Container(color: Colors.orange, width: 100, height: 100);
return Scaffold(
body: Column(
children: [
FadeTransition(
opacity: CurvedAnimation(parent: _controller, curve: _curve),
child: box,
),
SizedBox(height: 20),
AnimatedOpacity(
curve: _curve,
duration: _duration,
opacity: _flag ? 1 : 0,
child: box,
),
SizedBox(height: 20),
RaisedButton(
onPressed: () {
setState(() {
_flag = !_flag;
if (_flag) {
_controller.forward();
} else {
_controller.reverse();
}
});
},
child: Text(_flag ? 'Hide' : 'Show'),
),
],
),
);
}
输出:
如您所见,两个动画彼此不同步。我对FadeTransition 和AnimatedOpacity 使用相同的Curve 和Duration。但是,如果您删除 curve,动画会同步。那么,我在这里做错了什么?
【问题讨论】: