fromJson method

  1. @override
void fromJson(
  1. Map<String, dynamic> json
)
override

Implementation

@override
void fromJson(Map<String, dynamic> json) {
  if (json.containsKey("MindMapNode")) {
    Map<String, dynamic> nodeJson = json["MindMapNode"];
    if (nodeJson.containsKey("Properties")) {
      Map<String, dynamic> proJson = nodeJson["Properties"];

      if (proJson.containsKey("ID")) {
        setID(proJson["ID"].toString());
      }
      if (proJson.containsKey("Title")) {
        setTitle(proJson["Title"].toString());
      }
      if (proJson.containsKey("Extended")) {
        setExtended(proJson["Extended"].toString());
      }

      if (proJson.containsKey("Expanded")) {
        setExpanded(bool.tryParse(proJson["Expanded"].toString()) ?? true);
      }
      if (proJson.containsKey("Link")) {
        ILink? link = getMindMap()?.createLink(proJson["Link"].toString());
        if (link != null) {
          setLink(link);
        }
      }
      if (proJson.containsKey("LinkColor")) {
        setLinkColor(stringToColor(proJson["LinkColor"].toString()));
      }
      if (proJson.containsKey("LinkColors")) {
        if (proJson["LinkColors"] is List) {
          List<Color> list = [];
          for (String s in proJson["LinkColors"]) {
            list.add(stringToColor(s));
          }
          if (list.isNotEmpty) {
            setLinkColors(list);
          }
        }
      }
      if (proJson.containsKey("LinkWidth")) {
        setLinkWidth(double.tryParse(proJson["LinkWidth"]));
      }
      if (proJson.containsKey("BorderColors")) {
        if (proJson["BorderColors"] is List) {
          List<Color> list = [];
          for (String s in proJson["BorderColors"]) {
            list.add(stringToColor(s));
          }
          if (list.isNotEmpty) {
            setBorderColors(list);
          }
        }
      }
      if (proJson.containsKey("HSpace")) {
        setHSpace(int.tryParse(proJson["HSpace"]) ?? 50);
      }
      if (proJson.containsKey("VSpace")) {
        setVSpace(int.tryParse(proJson["VSpace"]) ?? 20);
      }
      if (proJson.containsKey("BackgroundColor")) {
        setBackgroundColor(stringToColor(proJson["BackgroundColor"]));
      }
      if (proJson.containsKey("Border")) {
        Color? topColor;
        double? topWidth;
        Color? leftColor;
        double? leftWidth;
        Color? bottomColor;
        double? bottomWidth;
        Color? rightColor;
        double? rightWidth;
        Map<String, dynamic> borderJson = proJson["Border"];
        if (borderJson.containsKey("Top")) {
          Map<String, dynamic> topJson = borderJson["Top"];
          if (topJson.containsKey("Color")) {
            topColor = stringToColor(topJson["Color"]);
          }
          if (topJson.containsKey("Width")) {
            topWidth = double.tryParse(topJson["Width"]);
          }
        }
        if (borderJson.containsKey("Left")) {
          Map<String, dynamic> leftJson = borderJson["Left"];
          if (leftJson.containsKey("Color")) {
            leftColor = stringToColor(leftJson["Color"]);
          }
          if (leftJson.containsKey("Width")) {
            leftWidth = double.tryParse(leftJson["Width"]);
          }
        }
        if (borderJson.containsKey("Bottom")) {
          Map<String, dynamic> bottomJson = borderJson["Bottom"];
          if (bottomJson.containsKey("Color")) {
            bottomColor = stringToColor(bottomJson["Color"]);
          }
          if (bottomJson.containsKey("Width")) {
            bottomWidth = double.tryParse(bottomJson["Width"]);
          }
        }
        if (borderJson.containsKey("Right")) {
          Map<String, dynamic> rightJson = borderJson["Right"];
          if (rightJson.containsKey("Color")) {
            rightColor = stringToColor(rightJson["Color"]);
          }
          if (rightJson.containsKey("Width")) {
            rightWidth = double.tryParse(rightJson["Width"]);
          }
        }

        setBorder(
          Border(
            top: topColor == null || topWidth == null || topWidth <= 0
                ? BorderSide.none
                : BorderSide(
                    color: topColor,
                    width: topWidth,
                    style: BorderStyle.solid,
                  ),
            left: leftColor == null || leftWidth == null || leftWidth <= 0
                ? BorderSide.none
                : BorderSide(
                    color: leftColor,
                    width: leftWidth,
                    style: BorderStyle.solid,
                  ),
            bottom:
                bottomColor == null || bottomWidth == null || bottomWidth <= 0
                ? BorderSide.none
                : BorderSide(
                    color: bottomColor,
                    width: bottomWidth,
                    style: BorderStyle.solid,
                  ),
            right: rightColor == null || rightWidth == null || rightWidth <= 0
                ? BorderSide.none
                : BorderSide(
                    color: rightColor,
                    width: rightWidth,
                    style: BorderStyle.solid,
                  ),
          ),
        );
      }
      if (proJson.containsKey("BorderRadius")) {
        setBorderRadius(
          BorderRadius.circular(
            double.tryParse(proJson["BorderRadius"].toString()) ?? 0,
          ),
        );
      }
      if (proJson.containsKey("Padding")) {
        Map<String, dynamic> padJson = proJson["Padding"];
        double? left;
        double? top;
        double? right;
        double? bottom;
        if (padJson.containsKey("Left")) {
          left = double.tryParse(padJson["Left"]);
        }
        if (padJson.containsKey("Top")) {
          top = double.tryParse(padJson["Top"]);
        }
        if (padJson.containsKey("Right")) {
          right = double.tryParse(padJson["Right"]);
        }
        if (padJson.containsKey("Bottom")) {
          bottom = double.tryParse(padJson["Bottom"]);
        }
        if (left != null || top != null || right != null || bottom != null) {
          setPadding(
            EdgeInsets.fromLTRB(left ?? 0, top ?? 0, right ?? 0, bottom ?? 0),
          );
        }
      }
      if (proJson.containsKey("TextStyle")) {
        Map<String, dynamic> tsJson = proJson["TextStyle"];
        Color? color;
        double? fontSize;
        bool? bold;
        if (tsJson.containsKey("Color")) {
          color = stringToColor(tsJson["Color"]);
        }
        if (tsJson.containsKey("FontSize")) {
          fontSize = double.tryParse(tsJson["FontSize"]);
        }
        if (tsJson.containsKey("Bold")) {
          bold = bool.tryParse(tsJson["Bold"].toString());
        }
        if (color != null || fontSize != null || bold != null) {
          setTextStyle(
            TextStyle(
              color: color,
              fontSize: fontSize,
              fontWeight: bold == true ? FontWeight.bold : FontWeight.normal,
            ),
          );
        }
      }
      if (proJson.containsKey("LinkInOffsetMode")) {
        for (MindMapNodeLinkOffsetMode o
            in MindMapNodeLinkOffsetMode.values) {
          if (o.name == proJson["LinkInOffsetMode"]) {
            setLinkInOffsetMode(o);
            break;
          }
        }
      }
      if (proJson.containsKey("LinkOutOffsetMode")) {
        for (MindMapNodeLinkOffsetMode o
            in MindMapNodeLinkOffsetMode.values) {
          if (o.name == proJson["LinkOutOffsetMode"]) {
            setLinkOutOffsetMode(o);
            break;
          }
        }
      }
    }
    if (nodeJson.containsKey("Nodes")) {
      Map<String, dynamic> nodes = nodeJson["Nodes"];
      if (nodes.containsKey("Left")) {
        for (Map<String, dynamic> l in nodes["Left"]) {
          if (l.isNotEmpty) {
            IMindMapNode? n = getMindMap()?.createNode(l.keys.first);
            if (n != null) {
              addLeftItem(n);
              n.fromJson(l);
            }
          }
        }
      }
      if (nodes.containsKey("Right")) {
        for (Map<String, dynamic> r in nodes["Right"]) {
          if (r.isNotEmpty) {
            IMindMapNode? n = getMindMap()?.createNode(r.keys.first);
            if (n != null) {
              addRightItem(n);
              n.fromJson(r);
            }
          }
        }
      }
    }
  }
}