paint method
Paint this render object into the given context at the given offset.
Subclasses should override this method to provide a visual appearance for themselves. The render object's local coordinate system is axis-aligned with the coordinate system of the context's canvas and the render object's local origin (i.e, x=0 and y=0) is placed at the given offset in the context's canvas.
Do not call this function directly. If you wish to paint yourself, call
markNeedsPaint instead to schedule a call to this function. If you wish
to paint one of your children, call PaintingContext.paintChild on the
given context.
When painting one of your children (via a paint child function on the given context), the current canvas held by the context might change because draw operations before and after painting children might need to be recorded on separate compositing layers.
Implementation
@override
void paint(PaintingContext context, Offset offset) {
final child = this.child;
if (child == null || size.isEmpty) return;
final tile = tileOf(size);
if (tile.isEmpty) return;
// A `space`d axis repeats the tile and the room after it: one cell of
// the pattern is the two.
final (gx, gy) = _spacing(size, tile.width, tile.height);
final cell = Size(tile.width + (gx ?? 0), tile.height + (gy ?? 0));
if (child.needsCompositing) {
_paintLive(context, offset, child, tile, cell, (gx, gy));
return;
}
_live.layer = null;
final image = _tile(child, tile.size, cell);
if (image == null) return;
// The box's corner and far corner on the page, each snapped to the
// nearest CSS pixel (half up) as Blink snaps the box's paint rect before
// the device scale: the tiles start at the one and are cut at both.
final corner = localToGlobal(Offset.zero);
double snap(double v) => (v + 0.5).floorToDouble();
final left = snap(corner.dx) - corner.dx, top = snap(corner.dy) - corner.dy;
final right = snap(corner.dx + size.width) - corner.dx, bottom = snap(corner.dy + size.height) - corner.dy;
if (right <= left || bottom <= top) return;
final matrix = Matrix4.translationValues(offset.dx + left + tile.left, offset.dy + top + tile.top, 0)
..scaleByDouble(cell.width / image.width, cell.height / image.height, 1, 1);
// A `space` axis repeats where it holds two copies, and lays the tile
// once where it does not, whatever `repeat` says.
final rx = _spaceX ? gx != null : _repeatX, ry = _spaceY ? gy != null : _repeatY;
final paint = Paint()
..shader = ImageShader(
image,
rx ? TileMode.repeated : TileMode.decal,
ry ? TileMode.repeated : TileMode.decal,
matrix.storage,
filterQuality: _sampling(image, cell, _dpr),
);
if (_color case final c?) paint.colorFilter = ColorFilter.mode(c, _hole ? BlendMode.srcOut : BlendMode.srcIn);
context.canvas.drawRect(
Rect.fromLTRB(offset.dx + left, offset.dy + top, offset.dx + right, offset.dy + bottom),
paint,
);
}