Flutter 布局(四)- Baseline、FractionallySizedBox、IntrinsicHeight、IntrinsicWidth详解

本文主要介绍Flutter布局中的Baseline、FractionallySizedBox、IntrinsicHeight、IntrinsicWidth四种控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析。

1. Baseline

A widget that positions its child according to the child’s baseline.

1.1 简介

Baseline这个控件,做过移动端开发的都会了解过,一般文字排版的时候,可能会用到它。它的作用很简单,根据child的baseline,来调整child的位置。例如两个字号不一样的文字,希望底部在一条水平线上,就可以使用这个控件,是一个非常基础的控件。

关于字符的Baseline,可以看下下面这张图,这具体就涉及到了字体排版,感兴趣的同学可以自行了解。

Baseline

1.2 布局行为

Baseline控件布局行为分为两种情况:

  • 如果child有baseline,则根据child的baseline属性,调整child的位置;
  • 如果child没有baseline,则根据child的bottom,来调整child的位置。

1.3 继承关系

Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > Baseline

1.4 示例代码

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的布局行为。

Baseline样例

1.5 源码解析

const Baseline({
Key key,
@required this.baseline,
@required this.baselineType,
Widget child
})

1.5.1 属性解析

baseline:baseline数值,必须要有,从顶部算。

baselineType:bseline类型,也是必须要有的,目前有两种类型:

  • alphabetic:对齐字符底部的水平线;
  • ideographic:对齐表意字符的水平线。

1.5.2 源码

我们来看看源码中具体计算尺寸的这段代码

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数值的,存在的话,就取这个值,不存在的话,则取其高度。

整体的计算过程:

  1. 获取child的 baseline 值;
  2. 计算出top值,其为 baseline - childBaseline,这个值有可能为负数;
  3. 计算出Baseline控件尺寸,宽度为child的,高度则为 top + childSize.height。

1.6 使用场景

跟字符对齐相关的会用到,其他场景暂时没有想到。

2. FractionallySizedBox

A widget that sizes its child to a fraction of the total available space

2.1 简介

FractionallySizedBox控件会根据现有空间,来调整child的尺寸,所以说child就算设置了具体的尺寸数值,也不起作用。

2.2 布局行为

FractionallySizedBox的布局行为主要跟它的宽高因子两个参数有关,当参数为null或者有具体数值的时候,布局表现不一样。当然,还有一个辅助参数alignment,作为对齐方式进行布局。

  • 当设置了具体的宽高因子,具体的宽高则根据现有空间宽高 * 因子,有可能会超出父控件的范围,当宽高因子大于1的时候;
  • 当没有设置宽高因子,则填满可用区域;

2.3 继承关系

Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > FractionallySizedBox

2.4 示例代码

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,
),
),
)

运行效果如下所示

FractionallySizedBox例子

2.5 源码解析

const FractionallySizedBox({
Key key,
this.alignment = Alignment.center,
this.widthFactor,
this.heightFactor,
Widget child,
})

2.5.1 属性解析

alignment:对齐方式,不能为null。

widthFactor:宽度因子,跟之前介绍的控件类似,宽度乘以这个值,就是最后的宽度。

heightFactor:高度因子,用作计算最后实际高度的。

其中widthFactor和heightFactor都有一个规则

  • 如果不为null,那么实际的最大宽高度则为child的宽高乘以这个因子;
  • 如果为null,那么child的宽高则会尽量充满整个区域。

2.5.2 源码

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;
}

源代码中,根据宽高因子是否存在,来进行相对应的尺寸计算。这个过程非常简单,不再赘述。

2.6 使用场景

当需要在一个区域里面取百分比尺寸的时候,可以使用这个,比方说,高度40%宽度70%的区域。当然,AspectRatio也可以达到近似的效果。

3. IntrinsicHeight

A widget that sizes its child to the child’s intrinsic height.

3.1 简介

IntrinsicHeight的作用是调整child到固定的高度。这个控件笔者也是看了很久,不知道它的作用是什么,官方说这个很有用,但是应该尽量少用,因为其效率问题。

3.2 布局行为

这个控件的作用,是将可能高度不受限制的child,调整到一个合适并且合理的尺寸。

3.3 继承关系

Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > IntrinsicHeight

3.4 示例代码

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例子

当没有IntrinsicHeight包裹着,可以看到,第一三个Container高度是不受限制的,当外层套一个IntrinsicHeight,第一三个Container高度就调整到第二个一样的高度。

3.5 源码解析

构造函数如下:

const IntrinsicHeight({ Key key, Widget child })

3.5.1 属性解析

除了child,没有提供额外的属性。

3.5.2 源码

当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;

首先会检测是否只有一个高度值满足约束条件,如果不是的话,则返回一个最小的高度。然后调整尺寸。

3.6 使用场景

说老实话,不知道在什么场景使用,可以替代的控件也有的。谷歌说很有用,效率会有问题,建议一般的就别用了。

4. IntrinsicWidth

A widget that sizes its child to the child’s intrinsic width.

4.1 简介

IntrinsicWidth从描述看,跟IntrinsicHeight类似,一个是调整高度,一个是调整宽度。同样是会存在效率问题,能别使用就尽量别使用。

4.2 布局行为

IntrinsicWidth不同于IntrinsicHeight,它包含了额外的两个参数,stepHeight以及stepWidth。而IntrinsicWidth的布局行为跟这两个参数相关。

  • 当stepWidth不是null的时候,child的宽度将会是stepWidth的倍数,当stepWidth值比child最小宽度小的时候,这个值不起作用;
  • 当stepWidth为null的时候,child的宽度是child的最小宽度;
  • 当stepHeight不为null的时候,效果跟stepWidth相同;
  • 当stepHeight为null的时候,高度取最大高度。

4.3 继承关系

Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > IntrinsicWidth

4.4 示例代码

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,),
],
),
),
)

IntrinsicWidth例子

分别对stepWidth以及stepHeight设置不同的值,可以看到不同的效果,当step值比最小宽高小的时候,这个值其实是不起作用的。感兴趣的同学可以自己试试。

4.5 源码解析

构造函数

const IntrinsicWidth({ Key key, this.stepWidth, this.stepHeight, Widget child })

4.5.1 属性解析

stepWidth:可以为null,效果参看上面所说的布局行为。

stepHeight:可以为null,效果参看上面所说的布局行为。

4.5.2 源码

我们先来看看布局代码中_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来进行判断,但是注意其对待高度与宽度的表现略有差异。

4.6 使用场景

这个控件,说老实话,笔者还是不知道该在什么场景下使用,可能会有些特殊的场景。但是从IntrinsicWidth与IntrinsicHeight布局差异看,Flutter基础控件封的确实很随性,一些可有可无甚至是重复的控件,我觉得精简精简挺好的,哈哈。

点击关注,共同学习!
[安全狗的自我修养](https://mp.weixin.qq.com/s/E6Kp0fd7_I3VY5dOGtlD4w)

[github haidragon](https://github.com/haidragon)

https://github.com/haidragon

6. 参考

  1. Baseline class
  2. 基线
  3. FractionallySizedBox class
  4. IntrinsicHeight class
  5. IntrinsicWidth class

学习笔记-Flutter 布局(四)- Baseline、FractionallySizedBox、IntrinsicHeight、IntrinsicWidth详解...相关推荐

  1. qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)

    原博主博客地址:http://blog.csdn.net/qq21497936 本文章博客地址:http://blog.csdn.net/qq21497936/article/details/7851 ...

  2. 嵌入式知识-ARM裸机-学习笔记(9):SD卡启动详解(S5PV210)

    嵌入式知识-ARM裸机-学习笔记(9):SD卡启动详解(S5PV210) 一.SD卡介绍 1. SD卡背景知识和特点 SD卡.MMC卡.MicroSD.TF卡:这些卡其实内部就是Flash存储颗粒,比 ...

  3. mysql数据库select语句用法_mysql学习笔记之完整的select语句用法实例详解

    本文实例讲述了mysql学习笔记之完整的select语句用法.分享给大家供大家参考,具体如下: 本文内容: 完整语法 去重选项 字段别名 数据源 where group by having order ...

  4. oracle rac 环境配置文件,学习笔记:Oracle RAC spfile参数文件配置案例详解

    天萃荷净 rac中的spfile探讨,记录一下Oracle RAC搭建完成后关于spfile参数文件的配置案例,与更改RAC环境中参数文件的方法 今天朋友的的rac,因为被同事做数据库升级,分别在两个 ...

  5. 学习笔记-Flutter 布局(二)- Padding、Align、Center详解

    Flutter 布局(二)- Padding.Align.Center详解 本文主要介绍Flutter布局中的Padding.Align以及Center控件,详细介绍了其布局行为以及使用场景,并对源码 ...

  6. 【Ext.Net学习笔记】03:Ext.Net DirectEvents用法详解、DirectMethods用法详解

    Ext.Net通过DirectEvents进行服务器端异步的事件处理.[Ext.Net学习笔记]02:Ext.Net用法概览.Ext.Net MessageBus用法.Ext.Net布局 中已经简单的 ...

  7. mysql select语句详解_mysql学习笔记之完整的select语句用法实例详解

    本文实例讲述了mysql学习笔记之完整的select语句用法.分享给大家供大家参考,具体如下: 本文内容: 完整语法 去重选项 字段别名 数据源 where group by having order ...

  8. nndl学习笔记(二)反向传播公式推导与详解

    写在前面 反向传播回顾 为什么需要反向传播? 基本思想 算法流程 算法局限性 详细推导(核心:多元微积分的*链式法则*) 一些定义 1. 输出层的误差δL\delta^{L}δL 2. 利用下一层误差 ...

  9. 深度学习与智能故障诊断学习笔记(三)——RNN与LSTM推导详解

    1.RNN 1.1网络结构 标准神经网络的输入输出在不同例子中可能有不同的长度,在学习中并不共享从不同位置上学到的特征.因为标准神经网络的训练集是稳定的,即所有的特征域表达的内容是同一性质的,一旦交换 ...

最新文章

  1. 可突破任意ARP防火墙,以限制流量为目标的简单网络管理软件
  2. 重磅!2020中国高校毕业生月薪排名:清华第1,共计24高校月薪过万
  3. 【 English 】与个人品质有关的英语词汇
  4. linux下rpm包和命令使用简介
  5. LTE: 下行HARQ进程数目的来源。
  6. PowerShell 调用dll
  7. httpd反代 + tomcat cluster + redis会话保持
  8. symfony php 亿万,php – symfony中的内存不足错误
  9. html2张图片垂直居中,任意图片实现垂直居中的三种方法(兼容性还不错)
  10. 地震日记-2008-05-14
  11. bat调用vbs脚本
  12. 十款微信小程序源码分享
  13. 国产数据库及厂商介绍
  14. 命令行下获取公网IP地址汇总
  15. 求助计算机程序员,程序员用代码求救:几近绝望时竟是老本行救了他
  16. 苹果手机怎么一屏两用
  17. CentOS解决nginx autoindex 截断文件名,末尾出现乱码
  18. java hssffont_Java HSSFFont.setColor方法代碼示例
  19. 黑马C++笔记——模板(CPP)
  20. 搜索引擎蜘蛛 ajax,SEO中的搜索引擎蜘蛛技术探析

热门文章

  1. Redis05:Redis的高级特性:expire 生存时间、pipeline 管道、info命令、Redis的持久化、Redis 的安全策略、Redis监控命令-monitor
  2. 深受程序员鄙视的外行语录
  3. Python基础语法【列表】
  4. oracle存储过程调用http接口
  5. 电脑族的营养饮食食谱
  6. IC后端(五)TCL语言(1)
  7. ui vue 创建项目教程 并关闭语法_创建vue项目流程
  8. chcon mysql_Linux中的SELinux与chcon以及Samba实现【转】
  9. 找你妹+ipad+wifi,回顾那年的经典游戏
  10. python获取服务器系统时间,Python datetime获取详细时间