paintWithPattern method
After clipping the canvas to the shape we want, paint the Pattern on the rectangle
defined by the provided x
, y
, width
, height
.
Implementation
void paintWithPattern(
Canvas canvas, double x, double y, double width, double height) {
final maxDimension = max(width, height);
var rectSide = maxDimension / featuresCount / 2;
var horizontalSquaresCount = width / rectSide;
var verticalSquaresCount = height / rectSide;
final paint = Paint()
..style = PaintingStyle.fill
..color = bgColor;
canvas.drawRect(Rect.fromLTWH(x, y, width, height), paint);
var dx = 0.0, dy = 0.0;
final rectsPath = Path();
for (var j = 0; j < verticalSquaresCount; j++) {
for (var i = 0; i < horizontalSquaresCount; i++) {
if (i % 2 == 0 && j % 2 != 0 || i % 2 != 0 && j % 2 == 0) {
final rectX = x + dx;
final rectY = y + dy;
final innerRect = Rect.fromLTWH(rectX, rectY, rectSide, rectSide);
rectsPath.addRect(innerRect);
}
dx += rectSide;
}
dy += rectSide;
dx = 0;
}
paint
..style = PaintingStyle.fill
..color = fgColor;
canvas.drawPath(rectsPath, paint);
}