addImageFromCanvas method
Create an image from a Canvas and add it to the map with the given id.
The painter function receives a Canvas to draw on.
The width and height defines the size of the resulting image
in pixels.
Implementation
Future<void> addImageFromCanvas({
required String id,
required void Function(Canvas canvas) painter,
int width = 200,
int height = 200,
}) async {
final pictureRecorder = PictureRecorder();
final canvas = Canvas(pictureRecorder);
painter(canvas);
final picture = pictureRecorder.endRecording();
final image = await picture.toImage(width, height);
final bytes = await image.toByteData(format: ImageByteFormat.png);
if (bytes == null) {
debugPrint(
'addImageFromCanvas: Failed to convert image to bytes for id "$id".',
);
return;
}
await addImage(id, bytes.buffer.asUint8List());
}