toMap method

Map<String, dynamic> toMap()

Converts the shape to a Map for serialization (socket transmission).

Implementation

Map<String, dynamic> toMap() {
  // Convert color to hex string (RRGGBB format)
  final r = color.red;
  final g = color.green;
  final b = color.blue;
  final hexColor =
      '#${r.toRadixString(16).padLeft(2, '0')}${g.toRadixString(16).padLeft(2, '0')}${b.toRadixString(16).padLeft(2, '0')}';

  final map = <String, dynamic>{
    'type': shapeTypeToString(type),
    'color': hexColor,
    'thickness': thickness,
    'lineType': lineTypeToString(lineType),
  };

  if (points != null) {
    map['points'] = points!.map((p) => {'x': p.dx, 'y': p.dy}).toList();
  }

  if (start != null) {
    map['x1'] = start!.dx;
    map['y1'] = start!.dy;
  }

  if (end != null) {
    map['x2'] = end!.dx;
    map['y2'] = end!.dy;
  }

  if (text != null) {
    map['text'] = text;
    map['x'] = start?.dx ?? 0;
    map['y'] = start?.dy ?? 0;
  }

  if (fontFamily != null) {
    map['font'] = fontFamily;
  }

  if (fontSize != null) {
    map['fontSize'] = fontSize;
  }

  if (imageSrc != null) {
    map['src'] = imageSrc;
  }

  return map;
}