paintWithPattern method

void paintWithPattern(
  1. Canvas canvas,
  2. double x,
  3. double y,
  4. double width,
  5. double height,
)
override

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;
  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 dotsPath = Path();
  for (var j = 0; j < verticalSquaresCount; j++) {
    for (var i = 0; i < horizontalSquaresCount; i++) {
      paint
        ..style = PaintingStyle.fill
        ..color = fgColor;
      Rect oval = Rect.fromCircle(
          center: Offset(x + dx + rectSide / 2, y + dy + rectSide / 2),
          radius: rectSide / 4);
      dotsPath.addOval(oval);
      dx += rectSide;
    }
    dy += rectSide;
    dx = 0;
  }
  paint
    ..style = PaintingStyle.fill
    ..color = fgColor;
  canvas.drawPath(dotsPath, paint);
}