https://flutterchina.club/widgets/layout/
尺寸限制类容器用于限制容器大小,Flutter中提供了多种这样的容器,如ConstrainedBox
、SizedBox
、UnconstrainedBox
、AspectRatio
等,本节将介绍一些常用的。
ConstrainedBox
https://api.flutter.dev/flutter/widgets/ConstrainedBox-class.htmlConstrainedBox
用于对子组件添加额外的约束。例如,如果你想让子组件的最小高度是80像素,你可以使用const BoxConstraints(minHeight: 80.0)
作为子组件的约束。
定义
ConstrainedBox({
Key key,
@required this.constraints,
Widget child,
})
BoxConstraints
BoxConstraints用于设置限制条件,它的定义如下:
const BoxConstraints({
this.minWidth = 0.0, //最小宽度
this.maxWidth = double.infinity, //最大宽度(宽度尽可能大)
this.minHeight = 0.0, //最小高度
this.maxHeight = double.infinity //最大高度
})
ConstrainedBox(
constraints: BoxConstraints(
// maxWidth: double.infinity, //宽度尽可能大
maxWidth: 300.00,
minWidth: 100.0,
minHeight: 100.0,
maxHeight: 300.0,
),
child: Container(
color: Colors.green,
child: Text('hello flutter'),
),
);
BoxConstraints还定义了一些便捷的构造函数,用于快速生成特定限制规则的BoxConstraints。
BoxConstraints.expand()
可以生成一个尽可能大的用以填充另一个容器的BoxConstraints。//定义
BoxConstraints.expand({
double? width,
double? height,
}) : minWidth = width ?? double.infinity,
maxWidth = width ?? double.infinity,
minHeight = height ?? double.infinity,
maxHeight = height ?? double.infinity;
BoxConstraints.loose()
//定义
BoxConstraints.loose(Size size)
: minWidth = 0.0,
maxWidth = size.width,
minHeight = 0.0,
maxHeight = size.height;
BoxConstraints.tight(Size size)
生成给定大小的限制 ```dart //定义 BoxConstraints.tight(Size size) : minWidth = size.width, maxWidth = size.width, minHeight = size.height, maxHeight = size.height;
BoxConstraints.tight(Size(300.0, 100.0));
- **`BoxConstraints.tightFor({width, height})`** 生成给定大小的限制,
如果没有传参,等同于: `BoxConstraints()` .
```dart
//定义
BoxConstraints.tightFor({
double? width,
double? height,
}) : minWidth = width ?? 0.0,
maxWidth = width ?? double.infinity,
minHeight = height ?? 0.0,
maxHeight = height ?? double.infinity;
BoxConstraints.tightFor(width: 300.0, height: 100.0);
BoxConstraints.tightForFinite()
//定义
tightForFinite({
double width = double.infinity,
double height = double.infinity,
}) : minWidth = width != double.infinity ? width : 0.0,
maxWidth = width != double.infinity ? width : double.infinity,
minHeight = height != double.infinity ? height : 0.0,
maxHeight = height != double.infinity ? height : double.infinity;
多重限制
如果某一个组件有多个父级ConstrainedBox
限制,那么最终会是哪个生效?我们看一个例子:
ConstrainedBox(
constraints: BoxConstraints(minWidth: 60.0, minHeight: 60.0), //父
child: ConstrainedBox(
constraints: BoxConstraints(minWidth: 90.0, minHeight: 20.0),//子
child: redBox,
)
)
上面我们有父子两个ConstrainedBox
,他们的限制条件不同,运行后效果如图5-4所示:
最终显示效果是宽90,高60,也就是说是子ConstrainedBox
的minWidth
生效,而minHeight
是父ConstrainedBox
生效。单凭这个例子,我们还总结不出什么规律,我们将上例中父子限制条件换一下:
ConstrainedBox(
constraints: BoxConstraints(minWidth: 90.0, minHeight: 20.0),
child: ConstrainedBox(
constraints: BoxConstraints(minWidth: 60.0, minHeight: 60.0),
child: redBox,
)
)
运行效果如图5-5所示:
最终的显示效果仍然是90,高60,效果相同,但意义不同,因为此时minWidth
生效的是父ConstrainedBox
,而minHeight
是子ConstrainedBox
生效。
通过上面示例,我们发现有多重限制时,对于minWidth
和minHeight
来说,是取父子中相应数值较大的。实际上,只有这样才能保证父限制与子限制不冲突。
思考题:对于
maxWidth
和maxHeight
,多重限制的策略是什么样的呢? 答:父子中相应数值较小的。
UnconstrainedBox
UnconstrainedBox
不会对子组件产生任何限制,它允许其子组件按照其本身大小绘制。一般情况下,我们会很少直接使用此组件,但在”去除”多重限制的时候也许会有帮助,我们看下下面的代码:
ConstrainedBox(
constraints: BoxConstraints(minWidth: 60.0, minHeight: 100.0), //父
child: UnconstrainedBox( //“去除”父级限制
child: ConstrainedBox(
constraints: BoxConstraints(minWidth: 90.0, minHeight: 20.0),//子
child: redBox,
),
)
)
上面代码中,如果没有中间的UnconstrainedBox
,那么根据上面所述的多重限制规则,那么最终将显示一个90×100的红色框。但是由于UnconstrainedBox
“去除”了父ConstrainedBox
的限制,则最终会按照子ConstrainedBox
的限制来绘制redBox
,即90×20:
但是,读者请注意,UnconstrainedBox
对父组件限制的“去除”并非是真正的去除:上面例子中虽然红色区域大小是90×20,但上方仍然有80的空白空间。也就是说父限制的minHeight
(100.0)仍然是生效的,只不过它不影响最终子元素redBox
的大小,但仍然还是占有相应的空间,可以认为此时的父ConstrainedBox
是作用于子UnconstrainedBox
上,而redBox
只受子ConstrainedBox
限制,这一点请读者务必注意。
那么有什么方法可以彻底去除父ConstrainedBox
的限制吗?答案是否定的!所以在此提示读者,在定义一个通用的组件时,如果要对子组件指定限制,那么一定要注意,因为一旦指定限制条件,子组件如果要进行相关自定义大小时将可能非常困难,因为子组件在不更改父组件的代码的情况下无法彻底去除其限制条件。
在实际开发中,当我们发现已经使用SizedBox
或ConstrainedBox
给子元素指定了宽高,但是仍然没有效果时,几乎可以断定:已经有父元素已经设置了限制!举个例子,如Material组件库中的AppBar
(导航栏)的右侧菜单中,我们使用SizedBox
指定了loading按钮的大小,代码如下:
AppBar(
title: Text(title),
actions: <Widget>[
SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 3,
valueColor: AlwaysStoppedAnimation(Colors.white70),
),
)
],
)
上面代码运行后,效果如图5-7所示:
我们会发现右侧loading按钮大小并没有发生变化!这正是因为AppBar
中已经指定了actions
按钮的限制条件,所以我们要自定义loading按钮大小,就必须通过UnconstrainedBox
来“去除”父元素的限制,代码如下:
AppBar(
title: Text(title),
actions: <Widget>[
UnconstrainedBox(
child: SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 3,
valueColor: AlwaysStoppedAnimation(Colors.white70),
),
),
)
],
)
运行后效果如图5-8所示:
生效了!
SizedBox 固定宽高
https://docs.flutter.io/flutter/widgets/SizedBox-class.htmlSizedBox
一个特定大小的盒子。这个widget强制它的孩子有一个特定的宽度和高度。如果宽度或高度为NULL,则此widget将调整自身大小以匹配该维度中的孩子的大小。
定义:
SizedBox({
Key key,
this.width,
this.height,
Widget child
}) : super(key: key, child: child);
/// Creates a box that will become as large as its parent allows.
const SizedBox.expand({Key key, Widget child})
: width = double.infinity,
height = double.infinity,
super(key: key, child: child);
/// Creates a box that will become as small as its parent allows.
const SizedBox.shrink({Key key, Widget child})
: width = 0.0,
height = 0.0,
super(key: key, child: child);
SizedBox 可以没有子组件,但仍然会占用空间,所以 SizedBox 非常适合控制2个组件之间的空隙。
Column(
children: <Widget>[
Container(height: 30,color: Colors.blue,),
SizedBox(height: 10,),
Container(height: 30,color: Colors.red,),
],
)
实际上SizedBox
只是ConstrainedBox
的一个定制。
SizedBox(
width: 100.0,
height: 100.0,
child: Container(
color: Colors.green,
child: Text('hello flutter'),
),
);
//等同于:
ConstrainedBox(
constraints: BoxConstraints.tightFor(width: 80.0,height: 80.0),
child: A,
);
//等同于:
ConstrainedBox(
BoxConstraints(
minHeight: 80.0,
maxHeight: 80.0,
minWidth: 80.0,
maxWidth: 80.0
),
child: A,
);
而实际上ConstrainedBox
和SizedBox
都是通过RenderConstrainedBox
来渲染的,我们可以看到ConstrainedBox
和SizedBox
的createRenderObject()
方法都返回的是一个RenderConstrainedBox
对象:
@override
RenderConstrainedBox createRenderObject(BuildContext context) {
return new RenderConstrainedBox(
additionalConstraints: ...,
);
}
AspectRatio 固定宽高比
https://api.flutter.dev/flutter/widgets/AspectRatio-class.html
一个widget,试图将子widget的大小指定为某个特定的长宽比。定义:
AspectRatio({
Key key,
@required this.aspectRatio,
Widget child,
})
AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,Widget的高度是由宽度和比率决定的,类似于 BoxFit 中的 contain,按照固定碧比率去尽量占满区域。
如果在满足所有限制条件过后无法找到一个可行的尺寸,AspectRatio 最终将会优先适应布局限制条件,而忽略所设置的比率。
示例:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
color: Colors.green,
child: AspectRatio(
aspectRatio: 3.0,
child: Text('你'),
),
),
SizedBox(height: 10.0),
Container(
width: 100.0,
color: Colors.green,
child: AspectRatio(
aspectRatio: 3.0,
child: Text('你'),
),
),
SizedBox(height: 10.0),
Container(
height: 60.0,
color: Colors.green,
child: AspectRatio(
aspectRatio: 3.0,
child: Text('你'),
),
),
],
);
请记住让 AspectRatio 控件确定其子级的大小。
如果把 AspectRatio 设置像 Expanded 这种命令,它将被父级强制扩展。
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 60.0,
height: 60.0,
color: Colors.green,
child: AspectRatio(
aspectRatio: 3.0, //如果父级宽高都限制了,它就没有效果了
child: Text('你'),
),
),
SizedBox(height: 10.0),
Container(
width: 300.0,
height: 60.0,
color: Colors.green,
alignment: Alignment.bottomCenter,
child: AspectRatio(
aspectRatio: 3.0, //如果父级宽高都限制了,它就没有效果了
child: Text('你'),
),
),
],
);
FractionallySizedBox 固定百分比
https://api.flutter.dev/flutter/widgets/FractionallySizedBox-class.html
一个widget,它把它的子项放在可用空间的一小部分,可以根据父容器宽高的百分比来设置子组件宽高。定义:
FractionallySizedBox({
Key key,
this.alignment = Alignment.center,
this.widthFactor,
this.heightFactor,
Widget child,
})
示例:
Container(
width: 200.0,
height: 200.0,
color: Colors.green,
child: FractionallySizedBox(
alignment: Alignment.topLeft,
widthFactor: 0.5,
heightFactor: 0.2,
child: Container(color: Colors.grey),
),
),
FittedBox 类似图片的fit属性
https://api.flutter.dev/flutter/widgets/FittedBox-class.html
按自己的大小调整其子widget的大小和位置。定义:
FittedBox({
Key key,
this.fit = BoxFit.contain,
this.alignment = Alignment.center,
this.clipBehavior = Clip.hardEdge,
Widget child,
})
示例:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 100.0,
height: 100.0,
color: Colors.green,
child: FittedBox(
fit: BoxFit.contain, //fill contain cover fitWidth fitHeight none scaleDown
child: Image.network('https://js.pic88.com/upload/20201117/16055958796074449641'),
),
),
SizedBox(height: 10.0),
Container(
width: 100.0,
height: 100.0,
color: Colors.green,
child: FittedBox(
fit: BoxFit.contain,
alignment: Alignment.topLeft,
child: Image.network('https://js.pic88.com/upload/20201117/16055958796074449641'),
),
),
],
);
Baseline
https://api.flutter.dev/flutter/widgets/Baseline-class.html
根据子项的基线对它们的位置进行定位的widget。定义:
Baseline({
Key key,
@required this.baseline,
@required this.baselineType,
Widget child,
})
示例:
Container(
width: 200.0,
height: 200.0,
color: Colors.green,
child: Baseline(
baseline: 100.0, //文字基线 距父组件顶部 100.0距离
baselineType: TextBaseline.alphabetic, //alphabetic ideographic
child: Text('hello flutter'),
),
),
IntrinsicHeight
https://api.flutter.dev/flutter/widgets/IntrinsicHeight-class.html
一个widget,它将它的子widget的宽度调整其本身实际的宽度。
LimitedBox 指定最大宽高
https://api.flutter.dev/flutter/widgets/LimitedBox-class.html
一个当其自身不受约束时才限制其大小的盒子。定义如下:
LimitedBox({
Key key,
this.maxWidth = double.infinity,
this.maxHeight = double.infinity,
Widget child,
})
一些控件根据父级定义的约束,来设置他们的大小,但是 ListView、Row、Column等控件,很难使其子项的大小保持平衡。当一个控件,没有父级来限制它的大小时,如何给它一个默认的大小呢?
使用 LimitedBox 控件。
仅当 LimitedBox 的父级未绑定时,LimitedBox才会为其子级提供默认大小。
当窗口控件的父级约束大小时,LimitedBox无效;
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
width: 200.0,
height: 200.0,
color: Colors.green,
child: LimitedBox(
maxWidth: 100, //父级限制了,此处就无效了
maxHeight: 100,
child: Container(color: Colors.grey),
),
),
SizedBox(height: 5),
Container(
child: LimitedBox(
maxWidth: 100,
maxHeight: 100,
child: Container(color: Colors.grey),
),
),
],
);
与滚动方向无限制的 ListView 一起使用时,LimitedBox 的潜力将变得无线。
ListView.builder(
itemCount: 10,
itemBuilder: (context, index) {
return LimitedBox(
maxHeight: 100.0,
child: Container(
child: Container(color: Color(Base.getRandomColor())),
),
);
},
);
Offstage 显隐
https://api.flutter.dev/flutter/widgets/Offstage-class.html
一个布局widget,可以控制其子widget的显示和隐藏。定义:
Offstage({ Key key, this.offstage = true, Widget child })
示例:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 60,
color: Colors.green,
child: Offstage(
offstage: true, //默认为true,虽然看不见了,但是还占位
child: Text('hello'),
),
),
SizedBox(height: 5),
Container(
height: 60,
color: Colors.green,
child: Offstage(
offstage: false,
child: Text('hello'),
),
),
],
);
OverflowBox 溢出
https://api.flutter.dev/flutter/widgets/OverflowBox-class.html
对其子项施加不同约束的widget,它可能允许子项溢出父级。定义:
OverflowBox({
Key key,
this.alignment = Alignment.center,
this.minWidth,
this.maxWidth,
this.minHeight,
this.maxHeight,
Widget child,
}) : super(key: key, child: child);
示例:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 40,
color: Colors.green,
child: OverflowBox(
maxHeight: 40,
child: Text('hello\nhello\nhello\nhello\nhello\nhello'),
),
),
SizedBox(height: 50),
Container(
height: 40,
color: Colors.green,
child: OverflowBox(
alignment: Alignment.topCenter,
// maxHeight: double.infinity,
maxHeight: 55,
child: Text('hello\nhello\nhello\nhello\nhello\nhello'),
),
),
],
);
SizedOverflowBox
一个特定大小的widget,但是会将它的原始约束传递给它的孩子,它可能会溢出。定义:
SizedOverflowBox({
Key key,
@required this.size,
this.alignment = Alignment.center,
Widget child,
})
示例:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 40,
color: Colors.green,
child: SizedOverflowBox(
alignment: Alignment.topCenter,
size: Size(100, 40),
child: Text('hello hello hello hello hello hello hello hello'),
),
),
SizedBox(height: 50),
Container(
height: 40,
color: Colors.green,
child: SizedOverflowBox(
alignment: Alignment.topCenter,
size: Size(100, 40),
child: Text('hello\nhello\nhello\nhello\nhello\nhello'),
),
),
],
);
CustomSingleChildLayout
https://api.flutter.dev/flutter/widgets/CustomSingleChildLayout-class.html
一个自定义的拥有单个子widget的布局widget。