在其子项上施加其他约束的窗口小部件。
例如,如果您希望子级的最小高度为50.0逻辑像素,则可以将其const BoxConstraints(minHeight: 50.0)来约束
。
参数详解
属性 | 说明 |
child | 子组件 |
constraints | 约束子组件的 条件 |
属性constraints是 BoxConstraints类型的,所以这里简单介绍其属性 | |
minWidth | 最小宽度 |
maxWidth | 最大宽度 |
minHeight | 最小高度 |
maxHeight | 最大高度 |
除了常用的属性外,还有一些构造函数会经常用到,如下: | |
BoxConstraints.tight(Size size) | 设定 指定 宽带高约束(内容多少都这么大) |
BoxConstraints.loose(Size size) | 设定 最大 宽带高约束 |
BoxConstraints.tightFor({ double width, double height, }) | 设定宽带高约束(默认最小限制为0,最大限制为 无穷大) |
BoxConstraints.tightForFinite({ double width = double.infinity, double height = double.infinity, }) | 设定宽带高约束 |
BoxConstraints.expand({ double width, double height, }) | 设定宽带高约束(w,h空时,取最大) |
BoxConstraints lerp( BoxConstraints a, BoxConstraints b, double t ) | 设定宽带高约束,a与b之间 |
代码示例
body: ConstrainedBox(
constraints: const BoxConstraints.expand(),
// constraints: BoxConstraints(minHeight: 100),
// constraints: BoxConstraints(minWidth: 100),
// constraints: BoxConstraints.lerp(BoxConstraints(minHeight: 0), BoxConstraints(minHeight: 150), 0.5),
// constraints: BoxConstraints.loose(Size(30,120)),
// constraints: BoxConstraints.tight(Size(30,120)),
// constraints: BoxConstraints.lerp(a, b, t),
// constraints: BoxConstraints.expand(width: 150,height: 150),
// constraints: BoxConstraints.tightForFinite(),
child: Container(
color: Colors.green,
child: Text('Hello World!')
),
),
效果图
完整代码