在AnimatedContainer中添加子元素时,需要将它们包装在容器中,以使其能进行动画。在包装它们之前,可以使用现有的布局控件(如Row或Column)来放置它们。以下是将Row与AnimatedContainer一起使用的示例代码:
class ExampleScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Container(
width: 50,
height: 50,
color: Colors.red,
),
AnimatedContainer(
duration: Duration(seconds: 1),
width: 50,
height: 50,
color: Colors.green,
),
Container(
width: 50,
height: 50,
color: Colors.blue,
),
],
),
),
);
}
}
在上面的代码中,AnimatedContainer被嵌套在Row控件中,使其能够与其他子元素一起进行动画。