UDocShape.fromJson constructor

UDocShape.fromJson(
  1. Map<String, Object?> json
)

Implementation

factory UDocShape.fromJson(Map<String, Object?> json) {
  final List<Object?> raw = (json["pts"] as List<Object?>?) ?? const <Object?>[];
  final List<Offset> points = <Offset>[];
  for (int i = 0; i + 1 < raw.length; i += 2) {
    points.add(Offset((raw[i] as num?)?.toDouble() ?? 0, (raw[i + 1] as num?)?.toDouble() ?? 0));
  }
  UDocShapeKind kind = UDocShapeKind.pen;
  for (final UDocShapeKind value in UDocShapeKind.values) {
    if (value.name == json["kind"]) kind = value;
  }
  return UDocShape(
    id: (json["id"] as String?) ?? _shapeId(),
    kind: kind,
    points: points,
    pageIndex: (json["page"] as num?)?.toInt() ?? -1,
    blockIndex: (json["block"] as num?)?.toInt() ?? -1,
    strokeColor: Color((json["stroke"] as num?)?.toInt() ?? 0xFFE53935),
    fillColor: json["fill"] == null ? null : Color((json["fill"]! as num).toInt()),
    strokeWidth: (json["w"] as num?)?.toDouble() ?? 0.004,
    opacity: (json["o"] as num?)?.toDouble() ?? 1,
    dashed: json["dash"] == true,
    text: (json["text"] as String?) ?? "",
    fontSize: (json["fs"] as num?)?.toDouble() ?? 0.028,
    bold: json["bold"] == true,
    startMs: (json["start"] as num?)?.toInt(),
    endMs: (json["end"] as num?)?.toInt(),
    createdAt: DateTime.tryParse((json["created"] as String?) ?? "") ?? DateTime.now(),
    updatedAt: DateTime.tryParse((json["updated"] as String?) ?? "") ?? DateTime.now(),
  );
}