介绍:
Container是一个组合类容器,它本身不对应具体的RenderObject,它是DecoratedBox、ConstrainedBox、Transform、Padding、Align等组件组合的一个多功能容器,所以我们只需通过一个Container组件可以实现同时需要装饰、变换、限制的场景。
列表参数:
Container({
this.alignment, //控制child的对齐方式,如果container或者container父节点尺寸大于child的尺寸,这个属性设置会起作用,有很多种对齐方式。
this.padding, //容器内补白,属于decoration的装饰范围
Color color, // 背景色
Decoration decoration, // 背景装饰
Decoration foregroundDecoration, //前景装饰,如果foregroundDecoration设置的话,可能会遮盖color效果。
double width,//容器的宽度
double height, //容器的高度
BoxConstraints constraints, //容器大小的限制条件
this.margin,//容器外补白,不属于decoration的装饰范围
this.transform, //变换
this.child,
})
绘制过程:
transform
decoration
paints the child //孩子组件
paints the foregroundDecoration //前景
综合练习:
import 'package:flutter/material.dart';
void main() {
runApp(FlutterContainer());
}
class FlutterContainer extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Center(
child: Container(
child: Text(
"我是一个Container",
textDirection: TextDirection.ltr,
style: TextStyle(color: Colors.green),
),
width: 400,
height: 400,
constraints: BoxConstraints(
minHeight: 200,
minWidth: 200
),
margin: const EdgeInsets.all(10.0),
alignment: Alignment.center,
color: Colors.yellow,
)
);
}
}

显示效果