单一子元素组件有Container、Padding、Align、Center、FittedBox、AspectRatio、SingleChildScrollView、FractionallySizedBox、ConstrainedBox和Baseline等。
Container(
alignment: Alignment.center,
constraints: const BoxConstraints.expand(width: 100, height: 80),
decoration: BoxDecoration(
border: Border.all(color: Colors.green, style: BorderStyle.solid, width: 2.0),
image: const DecorationImage(image: AssetImage('images/home_icon.png')),
borderRadius: const BorderRadius.all(Radius.circular(30)),
boxShadow: const [
BoxShadow(
color: Colors.redAccent,
offset: Offset(20, 20),
blurRadius: 10,
),
],
),
transform: Matrix4.rotationZ(.3),
margin: const EdgeInsets.all(100.0),
child: const Text('我是傻逼',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 13, fontStyle: FontStyle.italic, color: Colors.grey)),
)
Container(
width: 100,
height: 50,
color: Colors.red,
child: const Padding(
padding: EdgeInsets.all(10.0),
child: Text('啊啊啊啊'),
),
)
Align(
child:Text('我是Align'),
heightFactor: 2.0,
widthFactor:2.0,
alignment:Alignment.center,
)
Center(
child:Text('我是Center'),
heightFactor:2.0,
widthFactor:2.0,
)
body: Column(
children:[
Container(
width: 300,
height: 100,
color: Colors.red,
child: const FittedBox(
fit: BoxFit.contain,
child: Text('BoxFix.contain', style: TextStyle(fontSize: 16),),
),
),
Container(
width:300,
height: 100,
color: Colors.green,
child: const FittedBox(
fit: BoxFit.cover,
child: Text('BoxFit.cover', style: TextStyle(fontSize: 16),),
),
),
Container(
width: 300,
height: 50,
color: Colors.blue,
child: const FittedBox(
fit: BoxFit.fill,
child: Text('BoxFit.fill', style: TextStyle(fontSize: 16),),
),
),
Container(
width: 300,
height: 100,
color: Colors.yellow,
child: const FittedBox(
fit: BoxFit.scaleDown,
child: Text('BoxFit.scaleDown', style: TextStyle(fontSize: 16),),
),
),
Container(
width: 300,
height: 30,
color: Colors.purple,
child: const FittedBox(
fit: BoxFit.fitHeight,
child: Text('BoxFit.fitHeight', style: TextStyle(fontSize: 16),),
),
),
]
)
Container(
width: 200,
color: Colors.blue,
child: AspectRatio(
aspectRatio: 2/1,
child: Container(
color: Colors.yellow,
),
),
),
Container(
width: 200,
height: 200,
color: Colors.blue,
child: AspectRatio(
aspectRatio: 2/1,
child: Container(
color: Colors.purple,
),
),
)
Container(
color: Colors.purple,
width: 100,
height: 100,
child: FractionallySizedBox(
widthFactor: 2.0,
heightFactor: 0.5,
alignment: Alignment.center,
child: Container(
color: Colors.green,
),
),
)
ConstrainedBox(
constraints: const BoxConstraints(
minWidth: 100,
minHeight: 100,
maxWidth: 100,
maxHeight: 100,
),
child: Container(
color: Colors.green,
width: 100,
height: 200,
),
)
属性 | 类型 | 说明 |
---|---|---|
baseline | double | 必填参数,从顶部开始计算 |
手机 | TextBaseline | 必填参数,baseline类型,有两种类型alphabetic表示对齐字符底部水平线,ideographic表示对齐表意字符的水平线 |