add dot grid background

This commit is contained in:
2025-07-12 18:41:13 +00:00
parent 44e1fd3abc
commit 2894016cb7
2 changed files with 30 additions and 4 deletions
+29 -3
View File
@@ -9,24 +9,50 @@ abstract class CanvasBackground {
/// draw the backround on this context. Implement this to have /// draw the backround on this context. Implement this to have
/// different kinds of backgrounds /// different kinds of backgrounds
void paint(Canvas canvas, Size canvasSize, double scale); void paint(Canvas canvas, Offset offset, double scale, Size canvasSize);
} }
class NoBackground extends CanvasBackground { class NoBackground extends CanvasBackground {
const NoBackground(); const NoBackground();
@override @override
void paint(Canvas canvas, Size canvasSize, double scale) {} void paint(Canvas canvas, Offset offset, double scale, Size canvasSize) {}
} }
class SingleColorBackround extends CanvasBackground { class SingleColorBackround extends CanvasBackground {
const SingleColorBackround(Color backgroundColor) : super(bgColor: backgroundColor); const SingleColorBackround(Color backgroundColor) : super(bgColor: backgroundColor);
@override @override
void paint(Canvas canvas, Size canvasSize, double scale) { void paint(Canvas canvas, Offset offset, double scale, Size canvasSize) {
final paint = Paint() final paint = Paint()
..color = bgColor ..color = bgColor
..style = PaintingStyle.fill; ..style = PaintingStyle.fill;
canvas.drawRect(Rect.fromLTWH(0, 0, canvasSize.width, canvasSize.height), paint); canvas.drawRect(Rect.fromLTWH(0, 0, canvasSize.width, canvasSize.height), paint);
} }
} }
class DotGridBackround extends CanvasBackground {
final double size;
final double spacing;
final Color color;
const DotGridBackround({this.size = 2.0, this.spacing = 50.0, this.color = Colors.black12}) : super();
@override
void paint(Canvas canvas, Offset offset, double scale, Size canvasSize) {
final paint = Paint()
..color = color
..strokeWidth = 1.0 * scale;
double scaledSpacing = spacing * scale;
double scaledSize = size * scale;
double startX = (offset.dx % scaledSpacing) - scaledSpacing;
double startY = (offset.dy % scaledSpacing) - scaledSpacing;
for (double x = startX; x < canvasSize.width + scaledSpacing; x += scaledSpacing) {
for (double y = startY; y < canvasSize.height + scaledSpacing; y += scaledSpacing) {
canvas.drawCircle(Offset(x, y), scaledSize, paint);
}
}
}
}
+1 -1
View File
@@ -216,7 +216,7 @@ class _CanvasRenderBox extends RenderBox
void paint(PaintingContext context, Offset canvasStartOffset) { void paint(PaintingContext context, Offset canvasStartOffset) {
assert(childCount == _ssPositions.length); assert(childCount == _ssPositions.length);
// use the canvas background painter, pass it the canvas and that should handle drawing the background // use the canvas background painter, pass it the canvas and that should handle drawing the background
_canvasBackground.paint(context.canvas, size, _scale); _canvasBackground.paint(context.canvas, canvasStartOffset, _scale, size);
// though using ssPositionns here directly worked for me but docs using the parentData // though using ssPositionns here directly worked for me but docs using the parentData
// to get this info is the convention as child can be reordered ( though this will always // to get this info is the convention as child can be reordered ( though this will always