paint method
Paints the shape on the given canvas.
This method must be implemented by all concrete shape painters. It should handle the actual drawing logic for the specific shape.
canvas is the canvas to paint on.
size is the size of the canvas.
Implementation
@override
void paint(Canvas canvas, Size size) {
final center = Offset(size.width / 2, size.height / 2);
final rect = Rect.fromCenter(
center: center,
width: width,
height: height,
);
// Draw the filled rectangle
final paint = Paint()
..color = color
..style = PaintingStyle.fill;
if (cornerRadius != null) {
canvas.drawRRect(
RRect.fromRectAndRadius(rect, Radius.circular(cornerRadius!)),
paint,
);
} else {
canvas.drawRect(rect, paint);
}
// Draw the border if specified
if (strokeWidth != null && strokeColor != null) {
final borderPaint = Paint()
..color = strokeColor!
..style = PaintingStyle.stroke
..strokeWidth = strokeWidth!;
if (cornerRadius != null) {
canvas.drawRRect(
RRect.fromRectAndRadius(rect, Radius.circular(cornerRadius!)),
borderPaint,
);
} else {
canvas.drawRect(rect, borderPaint);
}
}
}