iOS- Flutter 布局组件-流式布局

128 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 17 天,点击查看活动详情

当子widget超过父widget的时候,则会报溢出错误:

Row(//换成Wrap则会自动换行
  children: <Widget>[
    Text("xxx"*100)
  ],
);

这个时候就需要做适配处理,比如显示不下则自动换行,这种即为流式布局。Flutter中通过wrap和flow来支持流式布局。

Wrap

Wrap({
  ...
  this.direction = Axis.horizontal,//方向
  this.alignment = WrapAlignment.start,//对齐类型
  this.spacing = 0.0,//主轴方向子Widget的间距
  this.runAlignment = WrapAlignment.start,//纵轴方向对齐类型
  this.runSpacing = 0.0,//纵轴间距
  this.crossAxisAlignment = WrapCrossAlignment.start,//表示子组件在纵轴方向的对齐方式
  this.textDirection,//表示水平方向子组件的布局顺序
  this.verticalDirection = VerticalDirection.down,//表示Row纵轴的对齐方向
  List<Widget> children = const <Widget>[],//Widget组件数组
})
Wrap(
   spacing: 8.0, // 主轴(水平)方向间距
   runSpacing: 4.0, // 纵轴(垂直)方向间距
   alignment: WrapAlignment.center, //沿主轴方向居中
   children: <Widget>[
     Chip(//剪接组件
       avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('A')),
       label: Text('Hamilton'),
     ),
     Chip(
       avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('M')),
       label: Text('Lafayette'),
     ),
     Chip(
       avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('H')),
       label: Text('Mulligan'),
     ),
     Chip(
       avatar: CircleAvatar(backgroundColor: Colors.blue, child: Text('J')),
       label: Text('Laurens'),
     ),
   ],
)

Flow

Flow需要自己实现子Widget的位置转换,一般优先考虑使用Wrap,Flow注意用于一些需要自定义布局策略或性能要求较高的场景,其实有点类似于iOS中自定义UICollectionViewLayout的意思。

FLow优点

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

缺点

  • 使用复杂
  • Flow不能自适应子组件大小,必须通过指定父容器大小或实现TestFlowDelegate的getSize返回固定大小。
Flow(
  delegate: TestFlowDelegate(margin: EdgeInsets.all(10.0)),
  children: <Widget>[
    Container(width: 80.0, height:80.0, color: Colors.red,),
    Container(width: 80.0, height:80.0, color: Colors.green,),
    Container(width: 80.0, height:80.0, color: Colors.blue,),
    Container(width: 80.0, height:80.0,  color: Colors.yellow,),
    Container(width: 80.0, height:80.0, color: Colors.brown,),
    Container(width: 80.0, height:80.0,  color: Colors.purple,),
  ],
)
//继承FlowDelegate
class TestFlowDelegate extends FlowDelegate {
  EdgeInsets margin;

  TestFlowDelegate({this.margin = EdgeInsets.zero});

  double width = 0;
  double height = 0;

  @override
  void paintChildren(FlowPaintingContext context) {
    var x = margin.left;
    var y = margin.top;
    //计算每一个子widget的位置
    for (int i = 0; i < context.childCount; i++) {
      var w = context.getChildSize(i)!.width + x + margin.right;
      if (w < context.size.width) {
        context.paintChild(i, transform: Matrix4.translationValues(x, y, 0.0));
        x = w + margin.left;
      } else {
        x = margin.left;
        y += context.getChildSize(i)!.height + margin.top + margin.bottom;
        //绘制子widget(有优化)
        context.paintChild(i, transform: Matrix4.translationValues(x, y, 0.0));
        x += context.getChildSize(i)!.width + margin.left + margin.right;
      }
    }
  }

  @override
  Size getSize(BoxConstraints constraints) {
    // 指定Flow的大小,简单起见我们让宽度竟可能大,但高度指定为200,
    // 实际开发中我们需要根据子元素所占用的具体宽高来设置Flow大小
    return Size(double.infinity, 200.0);
  }
//是否重绘
  @override
  bool shouldRepaint(FlowDelegate oldDelegate) {
    return oldDelegate != this;
  }
}