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嵌套很多GridView。

代码运行结果:

​​​​​​​

完整代码:

Dart 复制代码
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;
}
相关推荐
FungLeo10 小时前
Flutter fl_chart 0.70 破坏性 API 迁移实录:duration/getTooltipColor/withValues 全解
flutter·fl_chart
FungLeo16 小时前
Flutter 吸顶分组列表实战:语义桶分组 + 点击头平滑滚动
android·flutter
FungLeo17 小时前
Flutter 超长 StatefulWidget 拆分术:part of + extension on State 实战
android·flutter·dart
GitLqr18 小时前
Impeller 时代:Shader Jank 消失了,但渲染性能的战场也变了
flutter·面试·性能优化
恋猫de小郭21 小时前
Flutter 的另外一种形态?社区 DartNative 要来了。
android·前端·flutter
阿里云云原生2 天前
还原一次用户等待:深度解析 Flutter RUM SDK 如何打通 Dart 到 Native 的观测链路
flutter
_阿南_2 天前
flutter 展示的字体突然奔放了
android·flutter·ios
GitLqr2 天前
玩转 Dart typedef:不仅是函数别名那么简单
flutter·面试·dart
FungLeo2 天前
Flutter 两个反直觉布局坑:ListTile 水波纹 / VerticalDivider 踩坑实录
android·flutter
年小个大2 天前
受 go-zero 启发,我给 Flutter 写了一套 CLI 脚手架
android·flutter·架构