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

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

1. Padding

A widget that insets its child by the given padding.git

1.1 简介

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

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

if (margin != null)
  current = new Padding(padding: margin, child: current);
复制代码

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

1.2 布局行为

Padding的布局分为两种状况:函数

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

1.3 继承关系

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)的元素。ui

1.4 示例代码

实例代码直接上官方的例子,很是的简单:this

new Padding(
  padding: new EdgeInsets.all(8.0),
  child: const Card(child: const Text('Hello World!')),
)
复制代码

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

1.5 源码解析

构造函数以下:

const Padding({
    Key key,
    @required this.padding,
    Widget child,
  })
复制代码

包含一个padding属性,至关的简单。

1.5.1 属性解析

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

1.5.2 源码

@override
  RenderPadding createRenderObject(BuildContext context) {
    return new RenderPadding(
      padding: padding,
      textDirection: Directionality.of(context),
   );
}
复制代码

Padding的建立函数,其实是由RenderPadding来进行的。

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

if (child == null) {
  size = constraints.constrain(new Size(
    _resolvedPadding.left + _resolvedPadding.right,
    _resolvedPadding.top + _resolvedPadding.bottom
  ));
  return;
}
复制代码

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

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

// 调整child尺寸
final BoxConstraints innerConstraints = constraints.deflate(_resolvedPadding);
child.layout(innerConstraints, parentUsesSize: true);

// 调整child位置
final BoxParentData childParentData = child.parentData;
childParentData.offset = new Offset(_resolvedPadding.left, _resolvedPadding.top);

// 调整Padding尺寸
size = constraints.constrain(new Size(
  _resolvedPadding.left + child.size.width + _resolvedPadding.right,
  _resolvedPadding.top + child.size.height + _resolvedPadding.bottom
));
复制代码

到此处,上面介绍的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 继承关系

Object > Diagnosticable > DiagnosticableTree > Widget > RenderObjectWidget > SingleChildRenderObjectWidget > Align
复制代码

能够看出,Align跟Padding同样,也是一个很是基础的组件,Container中的align属性,也是使用Align去实现的。

2.4 示例代码

new Align(
  alignment: Alignment.center,
  widthFactor: 2.0,
  heightFactor: 2.0,
  child: new Text("Align"),
)
复制代码

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

2.5 源码解析

const Align({
    Key key,
    this.alignment: Alignment.center,
    this.widthFactor,
    this.heightFactor,
    Widget child
  })
复制代码

Align的构造函数基本上就是宽高因子、对齐方式属性。平常使用中,宽高因子属性基本上用的很少。若是是复杂的布局,Container内部的align属性也能够实现相同的效果。

2.5.1 属性解析

alignment:对齐方式,通常会使用系统默认提供的9种方式,可是并非说只有这9种,例如以下的定义。系统提供的9种方式只是预先定义好的。

/// The top left corner.
static const Alignment topLeft = const Alignment(-1.0, -1.0);
复制代码

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

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

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

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

2.5.2 源码

@override
  RenderPositionedBox createRenderObject(BuildContext context) {
    return new RenderPositionedBox(
      alignment: alignment,
      widthFactor: widthFactor,
      heightFactor: heightFactor,
      textDirection: Directionality.of(context),
    );
  }
复制代码

Align的实际构造调用的是RenderPositionedBox

RenderPositionedBox的布局表现以下:

// 根据_widthFactor、_heightFactor以及限制因素来肯定宽高
final bool shrinkWrapWidth = _widthFactor != null || constraints.maxWidth == double.infinity;
final bool shrinkWrapHeight = _heightFactor != null || constraints.maxHeight == double.infinity;

if (child != null) {
  //  若是child不为null,则根据规则设置Align的宽高,若是须要缩放,则根据_widthFactor是否为null来进行缩放,若是不须要,则尽可能扩展。
  child.layout(constraints.loosen(), parentUsesSize: true);
  size = constraints.constrain(new Size(shrinkWrapWidth ? child.size.width * (_widthFactor ?? 1.0) : double.infinity,
                                        shrinkWrapHeight ? child.size.height * (_heightFactor ?? 1.0) : double.infinity));
  alignChild();
} else {
  // 若是child为null,若是须要缩放,则变为0,不然就尽可能扩展
  size = constraints.constrain(new Size(shrinkWrapWidth ? 0.0 : double.infinity,
                                        shrinkWrapHeight ? 0.0 : double.infinity));
}
复制代码

2.6 使用场景

通常在对齐场景下使用,例如须要右对齐或者底部对齐之类的。它可以实现的功能,Container都能实现。

3. Center

Center继承自Align,只不过是将alignment设置为Alignment.center,其余属性例如widthFactor、heightFactor,布局行为,都与Align彻底同样,在这里就再也不单独作介绍了。Center源码以下,没有设置alignment属性,是由于Align默认的对齐方式就是居中。

class Center extends Align {
  /// Creates a widget that centers its child.
  const Center({ Key key, double widthFactor, double heightFactor, Widget child })
    : super(key: key, widthFactor: widthFactor, heightFactor: heightFactor, child: child);
}
复制代码

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
相关文章
相关标签/搜索