跳到正文
Willh的个人博客 记录 · 思考 · 分享
《Flutter实战·第二版》

4.5 流式布局

在介绍 Row 和 Colum 时,如果子 widget 超出屏幕范围,则会报溢出错误,如:

1Row(
2  children: <Widget>[
3    Text("xxx"*100)
4  ],
5);

运行效果如图4-6所示:

图4-6

可以看到,右边溢出部分报错。这是因为Row默认只有一行,如果超出屏幕不会折行。我们把超出屏幕显示范围会自动折行的布局称为流式布局。Flutter中通过Wrap和Flow来支持流式布局,将上例中的 Row 换成Wrap后溢出部分则会自动折行,下面我们分别介绍Wrap和Flow.

4.5.1 Wrap

下面是Wrap的定义:

1Wrap({
2  ...
3  this.direction = Axis.horizontal,
4  this.alignment = WrapAlignment.start,
5  this.spacing = 0.0,
6  this.runAlignment = WrapAlignment.start,
7  this.runSpacing = 0.0,
8  this.crossAxisAlignment = WrapCrossAlignment.start,
9  this.textDirection,
10  this.verticalDirection = VerticalDirection.down,
11  List<Widget> children = const <Widget>[],
12})

我们可以看到Wrap的很多属性在Row(包括Flex和Column)中也有,如direction、crossAxisAlignment、textDirection、verticalDirection等,这些参数意义是相同的,我们不再重复介绍,读者可以查阅前面介绍Row的部分。读者可以认为Wrap和Flex(包括Row和Column)除了超出显示范围后Wrap会折行外,其它行为基本相同。下面我们看一下Wrap特有的几个属性:

  • spacing:主轴方向子widget的间距
  • runSpacing:纵轴方向的间距
  • runAlignment:纵轴方向的对齐方式

下面看一个示例子:

1 Wrap(
2   spacing: 8.0, // 主轴(水平)方向间距
3   runSpacing: 4.0, // 纵轴(垂直)方向间距
4   alignment: WrapAlignment.center, //沿主轴方向居中
5   children: <Widget>[
6     Chip(
7       avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('A')),
8       label: Text('Hamilton'),
9     ),
10     Chip(
11       avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('M')),
12       label: Text('Lafayette'),
13     ),
14     Chip(
15       avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('H')),
16       label: Text('Mulligan'),
17     ),
18     Chip(
19       avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('J')),
20       label: Text('Laurens'),
21     ),
22   ],
23)

运行效果如图4-7所示:

图4-7

4.5.2 Flow

我们一般很少会使用Flow,因为其过于复杂,需要自己实现子 widget 的位置转换,在很多场景下首先要考虑的是Wrap是否满足需求。Flow主要用于一些需要自定义布局策略或性能要求较高(如动画中)的场景。Flow有如下优点:

  • 性能好;Flow是一个对子组件尺寸以及位置调整非常高效的控件,Flow用转换矩阵在对子组件进行位置调整的时候进行了优化:在Flow定位过后,如果子组件的尺寸或者位置发生了变化,在FlowDelegate中的paintChildren()方法中调用context.paintChild 进行重绘,而context.paintChild在重绘时使用了转换矩阵,并没有实际调整组件位置。
  • 灵活;由于我们需要自己实现FlowDelegate的paintChildren()方法,所以我们需要自己计算每一个组件的位置,因此,可以自定义布局策略。

缺点:

  • 使用复杂。
  • Flow 不能自适应子组件大小,必须通过指定父容器大小或实现TestFlowDelegate的getSize返回固定大小。

示例:

我们对六个色块进行自定义流式布局:

1Flow(
2  delegate: TestFlowDelegate(margin: EdgeInsets.all(10.0)),
3  children: <Widget>[
4    Container(width: 80.0, height:80.0, color: Colors.red,),
5    Container(width: 80.0, height:80.0, color: Colors.green,),
6    Container(width: 80.0, height:80.0, color: Colors.blue,),
7    Container(width: 80.0, height:80.0,  color: Colors.yellow,),
8    Container(width: 80.0, height:80.0, color: Colors.brown,),
9    Container(width: 80.0, height:80.0,  color: Colors.purple,),
10  ],
11)

实现TestFlowDelegate:

1class TestFlowDelegate extends FlowDelegate {
2  EdgeInsets margin;
3
4  TestFlowDelegate({this.margin = EdgeInsets.zero});
5
6  double width = 0;
7  double height = 0;
8
9  
10  void paintChildren(FlowPaintingContext context) {
11    var x = margin.left;
12    var y = margin.top;
13    //计算每一个子widget的位置
14    for (int i = 0; i < context.childCount; i++) {
15      var w = context.getChildSize(i)!.width + x + margin.right;
16      if (w < context.size.width) {
17        context.paintChild(i, transform: Matrix4.translationValues(x, y, 0.0));
18        x = w + margin.left;
19      } else {
20        x = margin.left;
21        y += context.getChildSize(i)!.height + margin.top + margin.bottom;
22        //绘制子widget(有优化)
23        context.paintChild(i, transform: Matrix4.translationValues(x, y, 0.0));
24        x += context.getChildSize(i)!.width + margin.left + margin.right;
25      }
26    }
27  }
28
29  
30  Size getSize(BoxConstraints constraints) {
31    // 指定Flow的大小,简单起见我们让宽度竟可能大,但高度指定为200,
32    // 实际开发中我们需要根据子元素所占用的具体宽高来设置Flow大小
33    return Size(double.infinity, 200.0);
34  }
35
36  
37  bool shouldRepaint(FlowDelegate oldDelegate) {
38    return oldDelegate != this;
39  }
40}

运行效果见图4-8:

可以看到我们主要的任务就是实现paintChildren,它的主要任务是确定每个子widget位置。由于Flow不能自适应子widget的大小,我们通过在getSize返回一个固定大小来指定Flow的大小。

注意,如果我们需要自定义布局策略,一般首选的方式是通过直接继承RenderObject,然后通过重写 performLayout 的方式实现,具体方式我们会在本书后面 Flutter 原理篇举例。