Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 42 additions & 28 deletions lib/models/draw_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ class DrawingPoint {
required this.paint,
this.pressure = 1.0,
});

Map<String, dynamic> toJson() {
return {
'offsetX': offset.dx,
'offsetY': offset.dy,
'color': paint.color.value,
'strokeWidth': paint.strokeWidth,
'strokeCap': paint.strokeCap.index,
'strokeJoin': paint.strokeJoin.index,
'pressure': pressure,
};
}

factory DrawingPoint.fromJson(Map<String, dynamic> json) {
final paint = Paint()
..color = Color(json['color'] as int)
..strokeWidth = (json['strokeWidth'] as num).toDouble()
..strokeCap = StrokeCap.values[json['strokeCap'] as int]
..strokeJoin = StrokeJoin.values[json['strokeJoin'] as int];

return DrawingPoint(
offset: Offset(
(json['offsetX'] as num).toDouble(),
(json['offsetY'] as num).toDouble(),
),
paint: paint,
pressure: (json['pressure'] as num?)?.toDouble() ?? 1.0,
);
}
}

class DrawPath {
Expand Down Expand Up @@ -52,17 +81,8 @@ class DrawPath {

Map<String, dynamic> toJson() {
return {
'points': points
.map((point) => {
'x': point.offset.dx,
'y': point.offset.dy,
'color': point.paint.color.toARGB32(),
'strokeWidth': point.paint.strokeWidth,
'pressure': point.pressure,
'style': point.paint.style.index, // Save paint style
})
.toList(),
'color': color.toARGB32(),
'points': points.map((point) => point.toJson()).toList(),
'color': color.value,
'strokeWidth': strokeWidth,
'strokeCap': strokeCap.index,
'isFill': isFill,
Expand All @@ -74,24 +94,18 @@ class DrawPath {
final List<dynamic> pointsList = json['points'];

return DrawPath(
points: pointsList.map((pointJson) {
return DrawingPoint(
offset: Offset(pointJson['x'], pointJson['y']),
paint: Paint()
..color = Color(pointJson['color'])
..strokeWidth = pointJson['strokeWidth']
..strokeCap = StrokeCap.round
..style = pointJson['style'] != null
? PaintingStyle.values[pointJson['style']]
: PaintingStyle.stroke, // Default to stroke for compatibility
pressure: pointJson['pressure'] ?? 1.0,
);
}).toList(),
color: Color(json['color']),
strokeWidth: json['strokeWidth'],
strokeCap: StrokeCap.values[json['strokeCap']],
points: pointsList
.map((pointJson) => DrawingPoint.fromJson(pointJson))
.toList(),
color: Color(json['color'] as int),
strokeWidth: (json['strokeWidth'] as num).toDouble(),
strokeCap: json['strokeCap'] != null
? StrokeCap.values[json['strokeCap'] as int]
: StrokeCap.round,
isFill: json['isFill'] ?? false,
brushType: BrushType.values[json['brushType'] ?? 0],
brushType: json['brushType'] != null
? BrushType.values[json['brushType'] as int]
: BrushType.brush,
);
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies:
image_picker: ^1.2.0
path_provider: ^2.1.5
path: ^1.9.1
bloc_test: ^10.0.0

dev_dependencies:
flutter_test:
Expand Down
Loading
Loading