canvasObjectFromJson function
Creates a retained canvas object from JSON.
Implementation
CanvasObject canvasObjectFromJson(Map<String, Object?> json) {
final paint = json['paint'] is Map
? CanvasPaint.fromJson(Map<String, Object?>.from(json['paint'] as Map))
: const CanvasPaint();
final id = json['id']?.toString();
final name = json['name']?.toString();
final locked = json['locked'] == true;
final hidden = json['hidden'] == true;
return switch (json['type']?.toString()) {
'rect' => CanvasRect(
id: id,
name: name,
paint: paint,
rotation: _toDouble(json['rotation']),
locked: locked,
hidden: hidden,
x: _toDouble(json['x']),
y: _toDouble(json['y']),
width: _toDouble(json['width']),
height: _toDouble(json['height']),
borderRadius: _toDouble(json['borderRadius']),
),
'line' => CanvasLine(
id: id,
name: name,
paint: paint,
rotation: _toDouble(json['rotation']),
locked: locked,
hidden: hidden,
x1: _toDouble(json['x1']),
y1: _toDouble(json['y1']),
x2: _toDouble(json['x2']),
y2: _toDouble(json['y2']),
),
'circle' => CanvasCircle(
id: id,
name: name,
paint: paint,
rotation: _toDouble(json['rotation']),
locked: locked,
hidden: hidden,
x: _toDouble(json['x']),
y: _toDouble(json['y']),
radius: _toDouble(json['radius']),
),
'text' => CanvasTextObject(
id: id,
name: name,
paint: paint,
rotation: _toDouble(json['rotation']),
locked: locked,
hidden: hidden,
text: json['text']?.toString() ?? '',
x: _toDouble(json['x']),
y: _toDouble(json['y']),
),
'image' => CanvasImageObject(
id: id,
name: name,
paint: paint,
rotation: _toDouble(json['rotation']),
locked: locked,
hidden: hidden,
src: json['src']?.toString() ?? '',
x: _toDouble(json['x']),
y: _toDouble(json['y']),
width: _toDouble(json['width']),
height: _toDouble(json['height']),
crossOrigin: json['crossOrigin']?.toString(),
),
_ => throw ArgumentError('Unknown canvas object type: ${json['type']}'),
};
}