flutter对数组中某类型数据二次加工成单独的数组

        如何将数据[2,1,2,2,2,1,2,2,3,2,2,2,2,3,2,2,2,2,2,3,2,4,2,2,1,2,3,2,4,2]加工成

[[2], 1, [2, 2, 2], 1, [2, 2], 3, [2, 2, 2, 2], 3, [2, 2, 2, 2, 2], 3, [2], 4, [2, 2], 1, [2], 3, [2], 4, [2]]。这是实际工作中遇到的问题,UI要求将某类型数据(如上述数组中的2)用宫格(GridView)展示,其他类型数据用列表(ListView)展示,实际结果就是在一个ListView中嵌套很N个GridView。

        代码运行结果:

         完整代码:

Future<void> main(List<String> args) async {
  List<int> intList = [2,1,2,2,2,1,2,2,3,2,2,2,2,3,2,2,2,2,2,3,2,4,2,2,1,2,3,2,4,2];
  arrayProcess(intList,2);
}
///intList,加工的数组
///加工的数据
List<Object> arrayProcess(List<int> intList,int num){
  List<Object> ints = [];
  int times = 1;
  List<int> temp = [];
  for(int i=0;i<intList.length;i++){
    if(i < (intList.length -1)){
      if(intList[i]==intList[i+1] && intList[i]==num){
        times= times+1;
        temp.add(intList[i]);
      }else {
        if(times>1){
          if(temp.length>0){
            temp.add(intList[i]);
            List<int> _temp = [];
            temp.forEach((element) {
              _temp.add(element);
            });
            ints.add(_temp);
            temp.clear();
          }

          times = 1;
        }
        else{
          if(intList[i]==num){
            temp.add(intList[i]);
            List<int> _temp = [];
            temp.forEach((element) {
              _temp.add(element);
            });
            ints.add(_temp);
            temp.clear();
          }
          else{
            ints.add(intList[i]);
            times = 1;
          }
        }
      }
    }
    else{
      if(times>1){
        if(temp.length>0){
          temp.add(intList[i]);
          List<int> _temp = [];
          temp.forEach((element) {
            _temp.add(element);
          });
          ints.add(_temp);
          temp.clear();
        }

        times = 1;
      }
      else{
        if(intList[i]==num){
          temp.add(intList[i]);
          List<int> _temp = [];
          temp.forEach((element) {
            _temp.add(element);
          });
          ints.add(_temp);
          temp.clear();
        }
        else{
          ints.add(intList[i]);
          times = 1;
        }
      }
    }
  }
  print(ints.toString());
  return ints;
}

        客户端展示:宫格展示UI要求一行展示两个,所以先根据记录数计算出GridView总高度,以确定高度,并且GridView是禁止滑动的,这样就可以在一个ListView插入N个GridView。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值