除了常規(guī)的設置 寬高外。Container默認是適配子控件大小的,但當設置對齊方式時Container將會填滿父控件,是否填滿剩余空間取決于子控件是否需要填滿父控件。
請看下面區(qū)別
- 情形1
class BodyDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Container(
color: Colors.blue,
height: 50,
width: 100,
),
Flexible(
child: Container(
color: Colors.red,
height: 50,
child: Text('Container',style: TextStyle(color: Colors.white),),
)
),
Container(
color: Colors.blue,
height: 50,
width: 100,
),
],
);
}
}
效果圖:

01.jpg
- 情形2
class BodyDemo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Container(
color: Colors.blue,
height: 50,
width: 100,
),
Flexible(
child: Container(
color: Colors.red,
height: 50,
alignment: Alignment.center,
child: Text('Container',style: TextStyle(color: Colors.white),),
)
),
Container(
color: Colors.blue,
height: 50,
width: 100,
),
],
);
}
}
效果圖:

02.jpg
二者唯一的差別之處就是: 是否設置了container的對齊方式alignment