编辑代码

import 'dart:convert';



void main() {
    MazeOptionHandler().init(x:3, y:3, mapList:map_list);
}

class MazeOptionHandler {
    bool isStart = false;
    int x = 0;
    int y = 0;
    List<MapItem> _mapList = [];

    List<List<ModelItem>> _dataList = [];

    init({int x,int y,List<MapItem> mapList}) {
        this.x = x;
        this.y = y;
        this._mapList = mapList;

        _createItems();
    }

    increment() {}
     
    /// 创建迷宫格
    _createItems(){
        int index = 1;
        List<ModelItem> items = [];
        for(int i = 0; i < x; i++){
            List<ModelItem> rows = [];
            for(int j = 0; j < y; j++){
                ModelItem item = ModelItem(xy: [i+1,j+1], index: index++);
                rows.add(item);
                items.add(item);
            }
            _dataList.add(rows);
        }

       print(json.encode( _dataList ));
    }
}

class ModelItem {
  /// Top Left, Top Right, Bottom Left, Bottom Right
  List<int> point = [0, 0, 0, 0];

  /// X, Y
  List<int> xy = [0, 0];

  int index = 0;

  /// Weight, Height
  int size = 0;

  /// 是否被选中
  bool afterLocation = false;
  /// 当前位置
  bool currentLocation = false;

  bool isStart = false;

  bool isEnd = false;

  ModelItem topNode;
  ModelItem leftNode;
  ModelItem rightNode;
  ModelItem bottomNode;



  ModelItem({this.xy,
    this.index,
    this.leftNode,
    this.rightNode,
    this.topNode,
    this.bottomNode});

  @override
  String toString(){
      return {
        "index": index
      }.toString();
  }

   Map<String, dynamic> toJson() => {
        "index": index,
        "isStart": isStart,
        "isEnd": isEnd,
        "topNode":topNode.toString(),
        "bottomNode":bottomNode.toString(),
        "leftNode":leftNode.toString(),
        "rightNode":rightNode.toString(),
    };
}

/// 地图类型
enum MapType{
    /// 向左
    Left,
    /// 向右
    Right,
    /// 向上
    Top,
    /// 向下
    Bottom,
    None,
    /// 开始
    Start,
    /// 结束
    End,
    /// 道路
    Road
}

/// 地图项
class MapItem {
    int fromIndex;
    MapType pathType;
    int toIndex;
    MapType stateType;

    MapItem({this.fromIndex,this.pathType,this.toIndex,this.stateType});

    @override
    toString(){
        return {
            "fromIndex": fromIndex,
            "pathType": pathType,
            "toIndex": toIndex,
            "stateType": stateType
        }.toString();
    }
}