美文网首页
Flutter 布局方式之流式布局

Flutter 布局方式之流式布局

作者: 小王在努力 | 来源:发表于2020-12-02 11:03 被阅读0次

前言

目前Flutter常见的布局方式主要有4种:1、线性布局 2、流式布局 3、弹性布局 4、层叠布局

今天我们主要介绍流式布局

线性布局

弹性布局

层叠布局

流式布局

我们把超出屏幕显示范围会自动换行的布局称为流式布局
关键字Wrap、Flow

流式布局Wrap关键字

 body: Wrap(
          direction: Axis.horizontal,
          children: [
            Container(
              margin: EdgeInsets.only(left: 30,top: 30),
              width: 100,
              height: 100,
              color: Colors.red,
              child: Text("1",style:TextStyle(
                color: Colors.white,
                fontSize: 60,
              )),
            ),
            Container(
              margin: EdgeInsets.only(left: 30,top: 30),
              width: 100,
              height: 100,
              color: Colors.yellow,
              child: Text("3",style:TextStyle(
                color: Colors.white,
                fontSize: 60,
              )),
            ),
            Container(
              margin: EdgeInsets.only(left: 30,top: 30),
              width: 100,
              height: 100,
              color: Colors.blue,
              child: Text("3",style:TextStyle(
                color: Colors.white,
                fontSize: 60,
              )),
            ),
            Container(
              margin: EdgeInsets.only(left: 30,top: 30),
              width: 100,
              height: 100,
              color: Colors.blue,
              child: Text("4",style:TextStyle(
                color: Colors.white,
                fontSize: 60,
              )),
            ),
          ],
        )
流式布局自动换行

流式布局Flow关键字

return Scaffold(
        appBar: AppBaseBar("流式布局"),
        body: Flow(
            delegate: MyFlowDelegate(boxSize: 80),
            children: List.generate(15, (index){
            return Box(index, 80);
          })
        )
    );

创建Box 代码

  /*一个正方形*/
  Widget Box(index,double boxSize) => Container(
    width: boxSize,
    height: boxSize,
    alignment: Alignment.center,
    color: Colors.red,
    child: Text(
      index.toString(),
      style: TextStyle(
        color: Colors.white,
        fontSize: 50,
        fontWeight: FontWeight.bold,
      ),
    ),
  );

delegate 实现

class MyFlowDelegate extends FlowDelegate {
  MyFlowDelegate({this.boxSize});
  final boxSize;
  @override
  void paintChildren(FlowPaintingContext context) {
    /*屏幕宽度*/
    var screenW = context.size.width;
    double padding = 5; //间距
    double offsetX = padding; //x坐标
    double offsetY = padding; //y坐标
    for (int i = 0; i < context.childCount; i++) {
      /*如果当前x左边加上子控件宽度小于屏幕宽度  则继续绘制  否则换行*/
      if (offsetX + boxSize < screenW) {
        /*绘制子控件*/
        context.paintChild(I,
            transform: Matrix4.translationValues(offsetX, offsetY, 0));
        /*更改x坐标*/
        offsetX = offsetX + boxSize + padding;
      } else {
        /*将x坐标重置为margin*/
        offsetX = padding;
        /*计算y坐标的值*/
        offsetY = offsetY + boxSize + padding;
        /*绘制子控件*/
        context.paintChild(I,
            transform: Matrix4.translationValues(offsetX, offsetY, 0));
      }
    }
  }
  @override
  bool shouldRepaint(FlowDelegate oldDelegate) {
    return true;
  }
}
UI效果显示

相关文章

  • Flutter 布局方式之流式布局

    前言 目前Flutter常见的布局方式主要有4种:1、线性布局 2、流式布局 3、弹性布局 4、层叠布局 今...

  • Flutter 布局方式之弹性布局

    前言 目前Flutter常见的布局方式主要有4种:1、线性布局 2、流式布局 3、弹性布局 4、层叠布局 今...

  • Flutter 布局方式之线性布局

    前言 目前Flutter常见的布局方式主要有4种:1、线性布局 2、流式布局 3、弹性布局 4、层叠布局 今天...

  • Flutter 布局方式之层叠布局

    前言 目前Flutter常见的布局方式主要有4种:1、线性布局 2、流式布局 3、弹性布局 4、层叠布局 今...

  • Flutter布局Widget--弹性布局、线性布局、流式布局和

    前言 本文我们要介绍 Flutter 中布局 Widget,包括弹性布局、线性布局流式布局和层叠布局。 一、弹性布...

  • Flutter 之 流式布局(Wrap、Flow)

    流式布局 流式布局在移动端是非常常见的,比如商品列表,瀑布流、标签页等等Flutter 中提供了两种流式布局Wra...

  • 流式布局

    流式布局指把超出屏幕显示范围会自动折行的布局,Flutter中通过Wrap和Flow来支持流式布局 Wrap Wr...

  • 布局小结

    布局方式 静态布局 浮动布局 定位布局 自适应布局 流式布局(百分比布局) 响应式布局 弹性布局 悬挂布局 圣杯布...

  • 响应式页面开发

    布局方式 固定布局 流式布局:流式布局是以百分比作为单位的,我们要牢记如下公式:百分比宽度 = 目标元素宽度 / ...

  • Flutter UI - Wrap、Flow 实现流式布局

    流式布局很经典了,Flutter 提供了2种方式,Wrap 用着省事,weightTree 里配置就行了,Flow...

网友评论

      本文标题:Flutter 布局方式之流式布局

      本文链接:https://www.haomeiwen.com/subject/bqcdwktx.html