本文主要介绍Flutter布局中的Baseline、FractionallySizedBox、IntrinsicHeight、IntrinsicWidth四种控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析。html
A widget that positions its child according to the child's baseline.git
Baseline这个控件,作过移动端开发的都会了解过,通常文字排版的时候,可能会用到它。它的做用很简单,根据child的baseline,来调整child的位置。例如两个字号不同的文字,但愿底部在一条水平线上,就可使用这个控件,是一个很是基础的控件。github
关于字符的Baseline,能够看下下面这张图,这具体就涉及到了字体排版,感兴趣的同窗能够自行了解。bash
Baseline控件布局行为分为两种状况:app
Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > Baseline
复制代码
new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Baseline(
baseline: 50.0,
baselineType: TextBaseline.alphabetic,
child: new Text(
'TjTjTj',
style: new TextStyle(
fontSize: 20.0,
textBaseline: TextBaseline.alphabetic,
),
),
),
new Baseline(
baseline: 50.0,
baselineType: TextBaseline.alphabetic,
child: new Container(
width: 30.0,
height: 30.0,
color: Colors.red,
),
),
new Baseline(
baseline: 50.0,
baselineType: TextBaseline.alphabetic,
child: new Text(
'RyRyRy',
style: new TextStyle(
fontSize: 35.0,
textBaseline: TextBaseline.alphabetic,
),
),
),
],
)
复制代码
上述运行结果是左右两个文本跟中间的Container底部在一个水平线上,这也印证了Baseline的布局行为。ide
const Baseline({
Key key,
@required this.baseline,
@required this.baselineType,
Widget child
})
复制代码
baseline:baseline数值,必需要有,从顶部算。函数
baselineType:bseline类型,也是必需要有的,目前有两种类型:布局
咱们来看看源码中具体计算尺寸的这段代码学习
child.layout(constraints.loosen(), parentUsesSize: true);
final double childBaseline = child.getDistanceToBaseline(baselineType);
final double actualBaseline = baseline;
final double top = actualBaseline - childBaseline;
final BoxParentData childParentData = child.parentData;
childParentData.offset = new Offset(0.0, top);
final Size childSize = child.size;
size = constraints.constrain(new Size(childSize.width, top + childSize.height));
复制代码
getDistanceToBaseline这个函数是获取baseline数值的,存在的话,就取这个值,不存在的话,则取其高度。字体
总体的计算过程:
跟字符对齐相关的会用到,其余场景暂时没有想到。
A widget that sizes its child to a fraction of the total available space
FractionallySizedBox控件会根据现有空间,来调整child的尺寸,因此说child就算设置了具体的尺寸数值,也不起做用。
FractionallySizedBox的布局行为主要跟它的宽高因子两个参数有关,当参数为null或者有具体数值的时候,布局表现不同。固然,还有一个辅助参数alignment,做为对齐方式进行布局。
Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > FractionallySizedBox
复制代码
new Container(
color: Colors.blue,
height: 150.0,
width: 150.0,
padding: const EdgeInsets.all(10.0),
child: new FractionallySizedBox(
alignment: Alignment.topLeft,
widthFactor: 1.5,
heightFactor: 0.5,
child: new Container(
color: Colors.red,
),
),
)
复制代码
运行效果以下所示
const FractionallySizedBox({
Key key,
this.alignment = Alignment.center,
this.widthFactor,
this.heightFactor,
Widget child,
})
复制代码
alignment:对齐方式,不能为null。
widthFactor:宽度因子,跟以前介绍的控件相似,宽度乘以这个值,就是最后的宽度。
heightFactor:高度因子,用做计算最后实际高度的。
其中widthFactor和heightFactor都有一个规则
FractionallySizedBox内部具体渲染是由RenderFractionallySizedOverflowBox来实现的,经过命名就能够看出,这个控件可能会Overflow。
咱们直接看实际计算尺寸的代码
double minWidth = constraints.minWidth;
double maxWidth = constraints.maxWidth;
if (_widthFactor != null) {
final double width = maxWidth * _widthFactor;
minWidth = width;
maxWidth = width;
}
double minHeight = constraints.minHeight;
double maxHeight = constraints.maxHeight;
if (_heightFactor != null) {
final double height = maxHeight * _heightFactor;
minHeight = height;
maxHeight = height;
}
复制代码
源代码中,根据宽高因子是否存在,来进行相对应的尺寸计算。这个过程很是简单,再也不赘述。
当须要在一个区域里面取百分比尺寸的时候,可使用这个,比方说,高度40%宽度70%的区域。固然,AspectRatio也能够达到近似的效果。
A widget that sizes its child to the child's intrinsic height.
IntrinsicHeight的做用是调整child到固定的高度。这个控件笔者也是看了好久,不知道它的做用是什么,官方说这个颇有用,可是应该尽可能少用,由于其效率问题。
这个控件的做用,是将可能高度不受限制的child,调整到一个合适而且合理的尺寸。
Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > IntrinsicHeight
复制代码
new IntrinsicHeight(
child: new Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
new Container(color: Colors.blue, width: 100.0),
new Container(color: Colors.red, width: 50.0,height: 50.0,),
new Container(color: Colors.yellow, width: 150.0),
],
),
);
复制代码
当没有IntrinsicHeight包裹着,能够看到,第一三个Container高度是不受限制的,当外层套一个IntrinsicHeight,第一三个Container高度就调整到第二个同样的高度。
构造函数以下:
const IntrinsicHeight({ Key key, Widget child })
复制代码
除了child,没有提供额外的属性。
当child不为null的时候,具体的布局代码以下:
BoxConstraints childConstraints = constraints;
if (!childConstraints.hasTightHeight) {
final double height = child.getMaxIntrinsicHeight(childConstraints.maxWidth);
assert(height.isFinite);
childConstraints = childConstraints.tighten(height: height);
}
child.layout(childConstraints, parentUsesSize: true);
size = child.size;
复制代码
首先会检测是否只有一个高度值知足约束条件,若是不是的话,则返回一个最小的高度。而后调整尺寸。
说老实话,不知道在什么场景使用,能够替代的控件也有的。谷歌说颇有用,效率会有问题,建议通常的就别用了。
A widget that sizes its child to the child's intrinsic width.
IntrinsicWidth从描述看,跟IntrinsicHeight相似,一个是调整高度,一个是调整宽度。一样是会存在效率问题,能别使用就尽可能别使用。
IntrinsicWidth不一样于IntrinsicHeight,它包含了额外的两个参数,stepHeight以及stepWidth。而IntrinsicWidth的布局行为跟这两个参数相关。
Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > IntrinsicWidth
复制代码
new Container(
color: Colors.green,
padding: const EdgeInsets.all(5.0),
child: new IntrinsicWidth(
stepHeight: 450.0,
stepWidth: 300.0,
child: new Column(
children: <Widget>[
new Container(color: Colors.blue, height: 100.0),
new Container(color: Colors.red, width: 150.0, height: 100.0),
new Container(color: Colors.yellow, height: 150.0,),
],
),
),
)
复制代码
分别对stepWidth以及stepHeight设置不一样的值,能够看到不一样的效果,当step值比最小宽高小的时候,这个值实际上是不起做用的。感兴趣的同窗能够本身试试。
构造函数
const IntrinsicWidth({ Key key, this.stepWidth, this.stepHeight, Widget child })
复制代码
stepWidth:能够为null,效果参看上面所说的布局行为。
stepHeight:能够为null,效果参看上面所说的布局行为。
咱们先来看看布局代码中_applyStep函数
static double _applyStep(double input, double step) {
assert(input.isFinite);
if (step == null)
return input;
return (input / step).ceil() * step;
}
复制代码
若是存在step数值的话,则会是step的倍数,若是step为null,则返回原始的尺寸。
接下来咱们看看child不为null时候的布局代码
BoxConstraints childConstraints = constraints;
if (!childConstraints.hasTightWidth) {
final double width = child.getMaxIntrinsicWidth(childConstraints.maxHeight);
assert(width.isFinite);
childConstraints = childConstraints.tighten(width: _applyStep(width, _stepWidth));
}
if (_stepHeight != null) {
final double height = child.getMaxIntrinsicHeight(childConstraints.maxWidth);
assert(height.isFinite);
childConstraints = childConstraints.tighten(height: _applyStep(height, _stepHeight));
}
child.layout(childConstraints, parentUsesSize: true);
size = child.size;
复制代码
宽度方面的布局跟IntrinsicHeight高度部分类似,只是多了一个step的额外数值。整体的布局表现跟上面分析的布局行为一致,根据step值是不是null来进行判断,可是注意其对待高度与宽度的表现略有差别。
这个控件,说老实话,笔者仍是不知道该在什么场景下使用,可能会有些特殊的场景。可是从IntrinsicWidth与IntrinsicHeight布局差别看,Flutter基础控件封的确实很随性,一些无关紧要甚至是重复的控件,我以为精简精简挺好的,哈哈。
笔者建了一个Flutter学习相关的项目,Github地址,里面包含了笔者写的关于Flutter学习相关的一些文章,会按期更新,也会上传一些学习Demo,欢迎你们关注。