Flutter 布局(二)- Padding、Align、Center详解

本文主要介绍Flutter布局中的Padding、Align以及Center控件,详细介绍了其布局行为以及使用场景,并对源码进行了分析。

1. Padding

A widget that insets its child by the given padding.

1.1 简介

Padding在Flutter中用的也挺多的,作为一个基础的控件,功能非常单一,给子节点设置padding属性。写过其他端的都了解这个属性,就是设置内边距属性,内边距的空白区域,也是widget的一部分。

Flutter中并没有单独的Margin控件,在Container中有margin属性,看源码关于margin的实现。

  1. if (margin != null)
  2. current = new Padding(padding: margin, child: current);

不难看出,Flutter中淡化了margin以及padding的区别,margin实质上也是由Padding实现的。

1.2 布局行为

Padding的布局分为两种情况:

  • 当child为空的时候,会产生一个宽为left+right,高为top+bottom的区域;
  • 当child不为空的时候,Padding会将布局约束传递给child,根据设置的padding属性,缩小child的布局尺寸。然后Padding将自己调整到child设置了padding属性的尺寸,在child周围创建空白区域。

1.3 继承关系

  1. Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > Padding

从继承关系可以看出,Padding控件是一个基础控件,不像Container这种组合控件。Container中的margin以及padding属性都是利用Padding控件去实现的。

1.3.1 关于SingleChildRenderObjectWidget

SingleChildRenderObjectWidget是RenderObjectWidgets的一个子类,用于限制只能有一个子节点。它只提供child的存储,而不提供实际的更新逻辑。

1.3.2 关于RenderObjectWidgets

RenderObjectWidgets为RenderObjectElement提供配置,而RenderObjectElement包含着(wrap)RenderObject,RenderObject则是在应用中提供实际的绘制(rendering)的元素。

1.4 示例代码

实例代码直接上官方的例子,非常的简单:

  1. new Padding(
  2. padding: new EdgeInsets.all(8.0),
  3. child: const Card(child: const Text('Hello World!')),
  4. )

例子中对Card设置了一个宽度为8的内边距。

1.5 源码解析

构造函数如下:

  1. const Padding({
  2. Key key,
  3. @required this.padding,
  4. Widget child,
  5. })

包含一个padding属性,相当的简单。

1.5.1 属性解析

padding:padding的类型为EdgeInsetsGeometry,EdgeInsetsGeometry是EdgeInsets以及EdgeInsetsDirectional的基类。在实际使用中不涉及到国际化,例如适配阿拉伯地区等,一般都是使用EdgeInsets。EdgeInsetsDirectional光看命名就知道跟方向相关,因此它的四个边距不限定上下左右,而是根据方向来定的。

1.5.2 源码

  1. @override
  2. RenderPadding createRenderObject(BuildContext context) {
  3. return new RenderPadding(
  4. padding: padding,
  5. textDirection: Directionality.of(context),
  6. );
  7. }

Padding的创建函数,实际上是由RenderPadding来进行的。

关于RenderPadding的实际布局表现,当child为null的时候:

  1. if (child == null) {
  2. size = constraints.constrain(new Size(
  3. _resolvedPadding.left + _resolvedPadding.right,
  4. _resolvedPadding.top + _resolvedPadding.bottom
  5. ));
  6. return;
  7. }

返回一个宽为_resolvedPadding.left+_resolvedPadding.right,高为_resolvedPadding.top+_resolvedPadding.bottom的区域。

当child不为null的时候,经历了三个过程,即调整child尺寸、调整child位置以及调整Padding尺寸,最终达到实际的布局效果。

  1. // 调整child尺寸
  2. final BoxConstraints innerConstraints = constraints.deflate(_resolvedPadding);
  3. child.layout(innerConstraints, parentUsesSize: true);
  4. // 调整child位置
  5. final BoxParentData childParentData = child.parentData;
  6. childParentData.offset = new Offset(_resolvedPadding.left, _resolvedPadding.top);
  7. // 调整Padding尺寸
  8. size = constraints.constrain(new Size(
  9. _resolvedPadding.left + child.size.width + _resolvedPadding.right,
  10. _resolvedPadding.top + child.size.height + _resolvedPadding.bottom
  11. ));

到此处,上面介绍的padding布局行为就解释的通了。

1.6 使用场景

Padding本身还是挺简单的,基本上需要间距的地方,它都能够使用。如果在单一的间距场景,使用Padding比Container的成本要小一些,毕竟Container里面包含了多个widget。Padding能够实现的,Container都能够实现,只不过,Container更加的复杂。

2. Align

A widget that aligns its child within itself and optionally sizes itself based on the child’s size.

2.1 简介

在其他端的开发,Align一般都是当做一个控件的属性,并没有拿出来当做一个单独的控件。Align本身实现的功能并不复杂,设置child的对齐方式,例如居中、居左居右等,并根据child尺寸调节自身尺寸。

2.2 布局行为

Align的布局行为分为两种情况:

  • 当widthFactor和heightFactor为null的时候,当其有限制条件的时候,Align会根据限制条件尽量的扩展自己的尺寸,当没有限制条件的时候,会调整到child的尺寸;
  • 当widthFactor或者heightFactor不为null的时候,Aligin会根据factor属性,扩展自己的尺寸,例如设置widthFactor为2.0的时候,那么,Align的宽度将会是child的两倍。

Align为什么会有这样的布局行为呢?原因很简单,设置对齐方式的话,如果外层元素尺寸不确定的话,内部的对齐就无法确定。因此,会有宽高因子、根据外层限制扩大到最大尺寸、外层不确定时调整到child尺寸这些行为。

2.3 继承关系

  1. Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > Align

可以看出,Align跟Padding一样,也是一个非常基础的组件,Container中的align属性,也是使用Align去实现的。

2.4 示例代码

  1. new Align(
  2. alignment: Alignment.center,
  3. widthFactor: 2.0,
  4. heightFactor: 2.0,
  5. child: new Text("Align"),
  6. )

例子依旧很简单,设置一个宽高为child两倍区域的Align,其child处在正中间。

2.5 源码解析

  1. const Align({
  2. Key key,
  3. this.alignment: Alignment.center,
  4. this.widthFactor,
  5. this.heightFactor,
  6. Widget child
  7. })

Align的构造函数基本上就是宽高因子、对齐方式属性。日常使用中,宽高因子属性基本上用的不多。如果是复杂的布局,Container内部的align属性也可以实现相同的效果。

2.5.1 属性解析

alignment:对齐方式,一般会使用系统默认提供的9种方式,但是并不是说只有这9种,例如如下的定义。系统提供的9种方式只是预先定义好的。

  1. /// The top left corner.
  2. static const Alignment topLeft = const Alignment(-1.0, -1.0);

Alignment实际上是包含了两个属性的,其中第一个参数,-1.0是左边对齐,1.0是右边对齐,第二个参数,-1.0是顶部对齐,1.0是底部对齐。根据这个规则,我们也可以自定义我们需要的对齐方式,例如

  1. /// 居右高于底部1/4处.
  2. static const Alignment rightHalfBottom = alignment: const Alignment(1.0, 0.5),

widthFactor:宽度因子,如果设置的话,Align的宽度就是child的宽度乘以这个值,不能为负数。

heightFactor:高度因子,如果设置的话,Align的高度就是child的高度乘以这个值,不能为负数。

2.5.2 源码

  1. @override
  2. RenderPositionedBox createRenderObject(BuildContext context) {
  3. return new RenderPositionedBox(
  4. alignment: alignment,
  5. widthFactor: widthFactor,
  6. heightFactor: heightFactor,
  7. textDirection: Directionality.of(context),
  8. );
  9. }

Align的实际构造调用的是RenderPositionedBox

RenderPositionedBox的布局表现如下:

  1. // 根据_widthFactor、_heightFactor以及限制因素来确定宽高
  2. final bool shrinkWrapWidth = _widthFactor != null || constraints.maxWidth == double.infinity;
  3. final bool shrinkWrapHeight = _heightFactor != null || constraints.maxHeight == double.infinity;
  4. if (child != null) {
  5. // 如果child不为null,则根据规则设置Align的宽高,如果需要缩放,则根据_widthFactor是否为null来进行缩放,如果不需要,则尽量扩展。
  6. child.layout(constraints.loosen(), parentUsesSize: true);
  7. size = constraints.constrain(new Size(shrinkWrapWidth ? child.size.width * (_widthFactor ?? 1.0) : double.infinity,
  8. shrinkWrapHeight ? child.size.height * (_heightFactor ?? 1.0) : double.infinity));
  9. alignChild();
  10. } else {
  11. // 如果child为null,如果需要缩放,则变为0,否则就尽量扩展
  12. size = constraints.constrain(new Size(shrinkWrapWidth ? 0.0 : double.infinity,
  13. shrinkWrapHeight ? 0.0 : double.infinity));
  14. }

2.6 使用场景

一般在对齐场景下使用,例如需要右对齐或者底部对齐之类的。它能够实现的功能,Container都能实现。

3. Center

Center继承自Align,只不过是将alignment设置为Alignment.center,其他属性例如widthFactor、heightFactor,布局行为,都与Align完全一样,在这里就不再单独做介绍了。Center源码如下,没有设置alignment属性,是因为Align默认的对齐方式就是居中。

  1. class Center extends Align {
  2. /// Creates a widget that centers its child.
  3. const Center({ Key key, double widthFactor, double heightFactor, Widget child })
  4. : super(key: key, widthFactor: widthFactor, heightFactor: heightFactor, child: child);
  5. }

4. 后话

笔者建了一个flutter学习相关的项目,github地址,里面包含了笔者写的关于flutter学习相关的一些文章,会定期更新,也会上传一些学习demo,欢迎大家关注。

5. 参考

  1. Padding class
  2. EdgeInsetsGeometry class
  3. EdgeInsets class
  4. EdgeInsetsDirectional class
  5. RenderPadding class
  6. Align class
  7. Center class