Flutter 瀑布流布局(根据图片高度展示)

Flutter瀑布流布局,根据后端返回的阿里云上的图片宽高信息来计算决定布局的数据信息

效果:
在这里插入图片描述

代码:

// 模拟数据
  var listData = [
    {'img':'https://img11.360buyimg.com/babel/s290x370_jfs/t1/168303/35/8655/261408/603db12dE885ad355/b7b88defb9b6d0e5.jpg!q95!cc_290x370.webp', 'width':290, 'height':370, 'ava_tar':'https://img1.baidu.com/it/u=873941767,2015595416&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500','name':'低调的丶奢华', 'like':132, 'desc':'京东高品质加点,最高可享24息免息,快来京东使用吧!!', },
    {'img':'https://img10.360buyimg.com/img/s130x130_jfs/t1/142212/38/23189/181038/61b40495Ee3f629ed/f1b89e383296fa4d.jpg!cc_130x130.webp', 'width':130, 'height':130, 'ava_tar':'https://img2.baidu.com/it/u=4244269751,4000533845&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500','name':'梦醒石乐芬', 'like':301, 'desc':'APPLE 苹果16英寸M1轻薄笔记本电脑教育优惠2021 深空灰 14英寸Pro8+14核】16G+512G', },
    {'img':'https://img20.360buyimg.com/da/s590x470_jfs/t1/120638/23/21032/80784/625d31caEacdcbd92/26534864d96aed1a.jpg.webp', 'width':500, 'height':300, 'ava_tar':'https://img0.baidu.com/it/u=1942253063,3807598283&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500','name':'爱吃西瓜的小美', 'like':79, 'desc':'高品质 | 快来用!', },
    {'img':'https://img20.360buyimg.com/babel/s290x370_jfs/t1/169223/11/9002/236742/603f38feEa4a8d83e/4ace79eafcac855c.jpg!cc_290x370.webp', 'width':290, 'height':370, 'ava_tar':'https://img1.baidu.com/it/u=4216761644,15569246&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500','name':'RNG丶Uzi', 'like':45, 'desc':'碧浪 Ariel 专业抗菌根源洁净香氛洗衣液13.6斤 杀菌除菌 家庭装套装机洗3KG*2+200G*4袋 补充装 包装随机', },
    {'img':'https://img11.360buyimg.com/babel/s290x370_jfs/t1/168303/35/8655/261408/603db12dE885ad355/b7b88defb9b6d0e5.jpg!q95!cc_290x370.webp', 'width':290, 'height':370, 'ava_tar':'https://img1.baidu.com/it/u=873941767,2015595416&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500','name':'低调的丶奢华', 'like':132, 'desc':'京东高品质加点,最高可享24息免息,快来京东使用吧!!', },
    {'img':'https://img10.360buyimg.com/img/s130x130_jfs/t1/142212/38/23189/181038/61b40495Ee3f629ed/f1b89e383296fa4d.jpg!cc_130x130.webp', 'width':130, 'height':130, 'ava_tar':'https://img2.baidu.com/it/u=4244269751,4000533845&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500','name':'梦醒石乐芬', 'like':301, 'desc':'APPLE 苹果16英寸M1轻薄笔记本电脑教育优惠2021 深空灰 14英寸Pro8+14核】16G+512G', },
    {'img':'https://img20.360buyimg.com/da/s590x470_jfs/t1/120638/23/21032/80784/625d31caEacdcbd92/26534864d96aed1a.jpg.webp', 'width':500, 'height':300, 'ava_tar':'https://img0.baidu.com/it/u=1942253063,3807598283&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500','name':'爱吃西瓜的小美', 'like':79, 'desc':'高品质 | 快来用!', },
    {'img':'https://img20.360buyimg.com/babel/s290x370_jfs/t1/169223/11/9002/236742/603f38feEa4a8d83e/4ace79eafcac855c.jpg!cc_290x370.webp', 'width':290, 'height':370, 'ava_tar':'https://img1.baidu.com/it/u=4216761644,15569246&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500','name':'RNG丶Uzi', 'like':45, 'desc':'碧浪 Ariel 专业抗菌根源洁净香氛洗衣液13.6斤 杀菌除菌 家庭装套装机洗3KG*2+200G*4袋 补充装 包装随机', },
  ];

  // 左侧一栏数据
  List<Widget> left_data = [];
  // 右侧一栏数据
  List<Widget> right_data = [];
  // 计算布局
_completeData(){
    for(var index = 0; index < listData.length; index++){
      if(index.isEven){
        if(int.parse(listData[index]['width'].toString()) < int.parse(listData[index]['height'].toString())){
          left_data.add(_createGridViewItem(height: 500, data:listData[index]));
        }else{
          left_data.add(_createGridViewItem(height:350, data:listData[index]));
        }
      }else{
        if(int.parse(listData[index]['width'].toString()) < int.parse(listData[index]['height'].toString())){
          right_data.add(_createGridViewItem(height: 500, data:listData[index]));
        }else{
          right_data.add(_createGridViewItem(height: 350, data:listData[index]));
        }
      }
    }
  }

// Widget布局
SafeArea(
      child:Scaffold(
        body: ConstrainedBox(
          constraints: BoxConstraints.expand(),
          child: Container(
            color: Color(0xffF7F7F7),
            padding: EdgeInsets.only(top:20),
            child: ListView(
              padding: EdgeInsets.only(top:0),
              children: [
                Container(
                  padding: EdgeInsets.symmetric(horizontal:10, vertical: 10)),
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: left_data
                        ),
                      ),
                      SizedBox(
                        width:10
                      ),
                      Expanded(
                        child: Column(
                          children: right_data
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        )
      )
    );
    
 _createGridViewItem({height, data}) {
    return Container(
      width:double.infinity,
      height: height,
      margin:EdgeInsets.only(bottom:10),
      color:Colors.white,
      child:Column(
        children: [
          Container(
            child:Image.network('${data['img']}', fit: BoxFit.fitWidth,),
          ),
          Expanded(
              child: Container(
            padding: EdgeInsets.symmetric(
                horizontal:20,
                vertical:10),
            decoration: BoxDecoration(
              color: Colors.white,
            ),
            child: Column(
              mainAxisAlignment:
                  MainAxisAlignment.spaceAround,
              crossAxisAlignment:
                  CrossAxisAlignment.start,
              children: [
                RichText(
                    maxLines: 2,
                    overflow:TextOverflow.ellipsis,
                    text: TextSpan(children: [
                      TextSpan(
                          text:
                              '${data['desc']}',
                          style: TextStyle(
                              fontSize:24,
                              fontWeight:
                                  FontWeight.bold,
                              color: Color(
                                  0xff434343),
                              letterSpacing: .8)),
                    ])),
                Container(
                    padding: EdgeInsets.symmetric(
                        vertical: 0),
                    child: Row(
                      mainAxisAlignment:
                          MainAxisAlignment
                              .spaceBetween,
                      children: [
                        Container(
                          child: Row(
                            children: [
                              ImageRoud(
                                  '${data['ava_tar']}',50),
                              Container(
                                  margin: EdgeInsets.only(
                                      left: 10),
                                  child: Text(
                                      '${data['name']}',
                                      style: TextStyle(
                                          fontSize:22,
                                          color: Color(
                                              0xffC5C5C5)))),
                            ],
                          ),
                        ),
                        Container(
                            child: Row(
                          children: [
                            Container(
                              margin: EdgeInsets.only(
                                  right: 10),
                              child: Icon(
                                  IconData(0xe6f8,
                                      fontFamily:
                                          'myIcon'),
                                  color: Color(
                                      0xffD7433F),
                                  size:20),
                            ),
                            Container(
                              child: Text('${data['like']}',
                                  style: TextStyle(
                                      color: Color(
                                          0xffC9C9C9),
                                      fontSize:24)),
                            )
                          ],
                        ))
                      ],
                    )),
              ],
            ),
          ))
        ],
      )
    );
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值