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;
}
相关推荐
我要最优解6 小时前
关于在mac中配置Java系统环境变量
java·flutter·macos
江上清风山间明月2 天前
Flutter开发的应用页面非常多时如何高效管理路由
android·flutter·路由·页面管理·routes·ongenerateroute
Zsnoin能2 天前
flutter国际化、主题配置、视频播放器UI、扫码功能、水波纹问题
flutter
早起的年轻人2 天前
Flutter CupertinoNavigationBar iOS 风格导航栏的组件
flutter·ios
HappyAcmen2 天前
关于Flutter前端面试题及其答案解析
前端·flutter
coooliang3 天前
Flutter 中的单例模式
javascript·flutter·单例模式
coooliang3 天前
Flutter项目中设置安卓启动页
android·flutter
JIngles1233 天前
flutter将utf-8编码的字节序列转换为中英文字符串
java·javascript·flutter
B.-3 天前
在 Flutter 中实现文件读写
开发语言·学习·flutter·android studio·xcode
freflying11193 天前
使用jenkins构建Android+Flutter项目依赖自动升级带来兼容性问题及Jenkins构建速度慢问题解决
android·flutter·jenkins