toImage method
Implementation
Future<ui.Image?> toImage() async {
if (isEmpty) return null;
final recorder = ui.PictureRecorder();
final canvas = Canvas(recorder);
final size = Size(double.infinity, widget.height);
// Draw background
canvas.drawRect(
Rect.fromLTWH(0, 0, size.width, size.height),
Paint()..color = widget.backgroundColor,
);
// Draw strokes
final paint = Paint()
..color = widget.penColor
..strokeWidth = widget.penWidth
..strokeCap = StrokeCap.round
..strokeJoin = StrokeJoin.round
..style = PaintingStyle.stroke;
for (final stroke in _strokes) {
if (stroke.length < 2) continue;
final path = Path()..moveTo(stroke.first.dx, stroke.first.dy);
for (var i = 1; i < stroke.length; i++) {
path.lineTo(stroke[i].dx, stroke[i].dy);
}
canvas.drawPath(path, paint);
}
final picture = recorder.endRecording();
return picture.toImage(400, widget.height.toInt());
}