【发布时间】:2021-03-12 07:09:45
【问题描述】:
如何从下到上更改容器的背景颜色? 当我使用渐变时,我不喜欢这种效果。 我构建了第二个容器,但它不是动态的。
class MyCustomContainer extends StatefulWidget {
final Color backgroundColor;
final Color progressColor;
final double progress;
final double size;
const MyCustomContainer({
Key key,
this.backgroundColor = Colors.grey,
this.progressColor = Colors.red,
@required this.progress,
@required this.size,
}) : super(key: key);
@override
_MyCustomContainerState createState() => _MyCustomContainerState();
}
class _MyCustomContainerState extends State<MyCustomContainer> {
double _progress;
@override
void initState() {
super.initState();
_progress = widget.progress;
}
onPaint() {
setState(() {
if (_progress == 0) {
_progress = 1;
} else {
_progress = 0;
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
height: widget.size,
width: widget.size,
child: Stack(
children: [
Container(
color: widget.backgroundColor,
child: Column(
children: [
Container(
height: widget.size * 0.3,
color: Colors.green,
),
Container(
height: widget.size * 0.7,
color: Colors.red,
),
],
),
),
Align(
alignment: Alignment.bottomCenter,
child: AnimatedContainer(
duration: Duration(milliseconds: 300),
curve: Curves.easeInOutExpo,
height: widget.size * _progress,
child: Column(children: [
Container(
height: widget.size * 0.3,
color: Colors.red,
),
Container(
height: widget.size * 0.7,
color: Colors.green,
),
]),
),
),
Text('asdas'),
],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => onPaint(),
child: Icon(Icons.ac_unit),
),
);
}
}
我想要这种过渡,一遍又一遍地改变两个容器的颜色。 如果状态发生变化,请进行转换,并在下一次迭代中更改颜色并再次播放。 这是要模拟的动画。 Video animation
知道怎么做吗?
【问题讨论】:
-
“我想要这种过渡,一遍又一遍地改变两个容器的颜色。” - 这就是
AnimatedContainer的用途(或TweenAnimationBuilder的用途动画自定义属性)
标签: flutter dart flutter-animation