draw method
Draws the contents or children of this Drawable to the canvas, using
the parentPaint to optionally override the child's paint.
The bounds specify the area to draw in.
Implementation
@override
void draw(Canvas canvas, Rect bounds) {
final Size imageSize = Size(
image.width.toDouble(),
image.height.toDouble(),
);
Size? desiredSize = imageSize;
double scale = 1.0;
if (size != null) {
desiredSize = size;
scale = math.min(
size!.width / image.width,
size!.height / image.height,
);
}
if (scale != 1.0 || offset != Offset.zero || transform != null) {
final Size halfDesiredSize = desiredSize! / 2.0;
final Size scaledHalfImageSize = imageSize * scale / 2.0;
final Offset shift = Offset(
halfDesiredSize.width - scaledHalfImageSize.width,
halfDesiredSize.height - scaledHalfImageSize.height,
);
canvas.save();
canvas.translate(offset.dx + shift.dx, offset.dy + shift.dy);
canvas.scale(scale, scale);
if (transform != null) {
canvas.transform(transform!);
}
}
canvas.drawImage(image, Offset.zero, Paint());
if (scale != 1.0 || offset != Offset.zero || transform != null) {
canvas.restore();
}
}