WhiteboardShape.fromMap constructor

WhiteboardShape.fromMap(
  1. Map<String, dynamic> map
)

Creates a shape from a Map (socket data).

Implementation

factory WhiteboardShape.fromMap(Map<String, dynamic> map) {
  final typeStr = map['type'] as String? ?? 'freehand';
  final type = stringToShapeType(typeStr);
  final colorStr = map['color'] as String? ?? '#000000';
  final color = _parseColor(colorStr);
  final thickness = (map['thickness'] as num?)?.toDouble() ?? 2.0;
  final lineTypeStr = map['lineType'] as String? ?? 'solid';
  final lineType = stringToLineType(lineTypeStr);

  List<Offset>? points;
  if (map['points'] != null) {
    points = (map['points'] as List)
        .map((p) => Offset(
              (p['x'] as num).toDouble(),
              (p['y'] as num).toDouble(),
            ))
        .toList();
  }

  Offset? start;
  if (map['x1'] != null && map['y1'] != null) {
    start = Offset(
      (map['x1'] as num).toDouble(),
      (map['y1'] as num).toDouble(),
    );
  } else if (map['x'] != null && map['y'] != null) {
    start = Offset(
      (map['x'] as num).toDouble(),
      (map['y'] as num).toDouble(),
    );
  }

  Offset? end;
  if (map['x2'] != null && map['y2'] != null) {
    end = Offset(
      (map['x2'] as num).toDouble(),
      (map['y2'] as num).toDouble(),
    );
  }

  return WhiteboardShape(
    type: type,
    points: points,
    start: start,
    end: end,
    text: map['text'] as String?,
    color: color,
    thickness: thickness,
    lineType: lineType,
    fontFamily: map['font'] as String?,
    fontSize: (map['fontSize'] as num?)?.toDouble(),
    imageSrc: map['src'] as String?,
  );
}