Flutter流式布局

Row(
  children: <Widget>[
     Text("大帅哥"*100),
   ],
 ),

在这里插入图片描述
如果子widget超出屏幕范围,则会报溢出错误。这是因为Row默认只有一行,如果超出屏幕不会折行。我们把超出屏幕显示范围会自动折行的布局称为流式布局。Flutter中通过Wrap和Flow来支持流式布局。

Wrap
spacing:

主轴方向子widget的间距

runSpacing:

纵轴方向的间距

runAlignment:

纵轴方向的对齐方式

body: Container(
        margin: const EdgeInsets.all(20),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,//交叉轴的排列方向
          children: <Widget>[
//            Row(
//              children: <Widget>[
//                Text("大帅哥"*100),
//              ],
//            ),

            Wrap(//标签超出自动换行展示
              spacing: 4,//水平方向间距
              runSpacing: 10,//垂直方向间距
              alignment: WrapAlignment.start,
              children: <Widget>[
                Chip(
                  label: Text("吸星大法"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.red,
                    child: Text("X"),
                  ),
                ),
                Chip(
                  label: Text("一阳指"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.red,
                    child: Text("Y"),
                  ),
                ),
                Chip(
                  label: Text("降龙十八掌"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.red,
                    child: Text("X"),
                  ),
                ),
                Chip(
                  label: Text("凌波微步"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.red,
                    child: Text("L"),
                  ),
                ),
                Chip(
                  label: Text("打狗棒法"),
                  avatar: CircleAvatar(
                    backgroundColor: Colors.red,
                    child: Text("D"),
                  ),
                ),

                Text("大帅哥"*30),
              ],
            ),
          ],
        ),
      ),

效果
在这里插入图片描述

Flow

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

优点:

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

缺点:

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

示例:

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

Flow(
  delegate: TestFlowDelegate(margin: EdgeInsets.all(10)),
  children: <Widget>[
    Container(
      width: 50,
      height: 50,
      color: Colors.red,
    ),
    Container(
      width: 50,
      height: 50,
      color: Colors.amber,
    ),
    Container(
      width: 50,
      height: 50,
      color: Colors.purple,
    ),
    Container(
      width: 50,
      height: 50,
      color: Colors.black,
    ),
    Container(
      width: 50,
      height: 50,
      color: Colors.blue,
    ),
    Container(
      width: 50,
      height: 50,
      color: Colors.greenAccent,
    ),
    Container(
      width: 50,
      height: 50,
      color: Colors.grey,
    ),
    Container(
      width: 50,
      height: 50,
      color: Colors.deepPurpleAccent,
    ),
  ],
),

Text("我在这"),

主要实现paintChildren,它主要确定每个子widget位置。由于Flow不能自适应子widget的大小,通过在getSize返回一个固定大小来指定Flow的大小。

class TestFlowDelegate extends FlowDelegate{
  EdgeInsets? margin = EdgeInsets.zero;
  TestFlowDelegate({
    this.margin
  });

  @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: new 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: new Matrix4.translationValues(
                x, y, 0.0));
        x += context.getChildSize(i)!.width + margin!.left + margin!.right;
      }
    }
  }


  @override
  Size getSize(BoxConstraints constraints) {
    //指定Flow的大小
    return Size(double.infinity,300);
  }

  @override
  bool shouldRepaint(covariant FlowDelegate oldDelegate) {
    return oldDelegate != this;
  }

}

运行效果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值