Flutter 动画容器 AnimatedContainer
warning:
这篇文章距离上次修改已过200天,其中的内容可能已经有所变动。
AnimatedContainer
是Flutter中的一个widget,它可以在动画的帮助下实现容器的变化。例如,你可以改变其颜色,尺寸,边框等属性。
以下是一些使用AnimatedContainer的示例:
- 改变颜色:
AnimatedContainer(
duration: Duration(seconds: 2),
curve: Curves.fastOutSlowIn,
color: Colors.blue,
)
在这个例子中,我们创建了一个AnimatedContainer
,当颜色改变时,变化是渐变的,持续时间是2秒,曲线是Curves.fastOutSlowIn
。
- 改变尺寸:
AnimatedContainer(
duration: Duration(seconds: 2),
curve: Curves.fastOutSlowIn,
width: 100,
height: 100,
)
在这个例子中,我们创建了一个AnimatedContainer
,当尺寸改变时,变化也是渐变的。
- 改变边框:
AnimatedContainer(
duration: Duration(seconds: 2),
curve: Curves.fastOutSlowIn,
width: 100,
height: 100,
decoration: BoxDecoration(
border: Border.all(color: Colors.blue, width: 5),
),
)
在这个例子中,我们创建了一个AnimatedContainer
,当边框改变时,变化也是渐变的。
- 使用AnimatedContainer创建一个点击按钮,改变颜色:
bool _changeColor = false;
AnimatedContainer(
duration: Duration(seconds: 2),
curve: Curves.fastOutSlowIn,
color: _changeColor ? Colors.blue : Colors.red,
)
RaisedButton(
child: Text('Change Color'),
onPressed: () {
setState(() {
_changeColor = !_changeColor;
});
},
)
在这个例子中,我们创建了一个AnimatedContainer
,当按下按钮时,颜色会发生变化。
注意:AnimatedContainer
的duration
和curve
属性是必需的,因为它们定义了动画的持续时间和曲线。
评论已关闭