Container是一個(gè)單一子元素容器恒傻,能夠設(shè)置背景,邊框建邓,寬高盈厘,內(nèi)外邊距等的容器。因此官边,Container是一個(gè)非撤惺郑基礎(chǔ)的組件,同時(shí)也是用途廣泛的組件
構(gòu)造函數(shù)
Container(
{Key key,
AlignmentGeometry alignment,
EdgeInsetsGeometry padding,
Color color,
Decoration decoration,
Decoration foregroundDecoration,
double width,
double height,
BoxConstraints constraints,
EdgeInsetsGeometry margin,
Matrix4 transform,
Widget child}
)
屬性
- alignment
child對(duì)齊的屬性注簿,可以設(shè)置居中、居左诡渴、居右捐晶、居上、居下等等妄辩。之前的Stack已經(jīng)介紹過(guò)了。 - padding: 內(nèi)邊界
- color:背景色
- decoration
設(shè)置裝飾眼耀,比如邊框英支、圓角、背景圖片等哮伟。不能給Container同時(shí)設(shè)置decoration和color屬性,如果要同時(shí)設(shè)置楞黄,給decoration設(shè)置color就可以池凄。 - foregroundDecoration:設(shè)置前景裝飾
- width:寬度。
- height:高度修赞。
- constraints:大小范圍約束,constraints有四個(gè)屬性minWidth、minHeight柏副、maxWidth勾邦、maxHeight。
- margin: 外邊距割择。
- transform:變換效果眷篇,比如翻轉(zhuǎn)、旋轉(zhuǎn)荔泳、變形等蕉饼。
- child:子組件,可以不設(shè)置玛歌。
示例代碼如下:
Stack(
fit: StackFit.expand,
children: [
Container(
alignment: AlignmentDirectional.center,
color: Colors.blue,
margin: const EdgeInsets.all(15.0),
child: Container(
alignment: AlignmentDirectional.topStart,
width: 100,
height: 100,
padding: const EdgeInsets.all(20.0),
constraints: const BoxConstraints.expand(width: 200.0, height: 200.0),
decoration: BoxDecoration(
border: Border.all(color: Colors.amber, width: 2.0),
borderRadius:
const BorderRadiusDirectional.all(Radius.circular(10.0))),
transform: Matrix4.rotationZ(0.1),
child: const Text('hello world'),
),
),
],
);
運(yùn)行效果:
關(guān)于width昧港,height,constraints對(duì)Container大小的影響關(guān)系
- 不設(shè)置寬高和約束支子,有child创肥,size會(huì)盡可能小。
- 不設(shè)置寬高和約束值朋,沒(méi)有child叹侄,size會(huì)盡可能大。
- 寬高不在約束范圍內(nèi)會(huì)無(wú)效昨登。