paint method
Paint the decoration. This paint is always done before the actual content is painted.
Implementation
void paint(Canvas canvas, Size size) {
// Width and height of the pattern
final patternHeight = size.height - padding.top - padding.bottom;
final patternWidth = size.width - padding.left - padding.right;
// There might be padding setting and layout constraints that lead negative
// heights or width. In this case nothing is painted
if (patternHeight <= 0.0 || patternWidth <= 0.0) {
debugPrint(
'Either pattenHeight ($patternHeight) or '
'patternWidth ($patternWidth) is less than 0. No pattern will be'
'painted.',
);
return;
}
// Starting points of the pattern
final double xStart = padding.left;
final double yStart = padding.top;
// If a customer painter is defined it is called and the functions returns.
if (type == PencilDecorationType.custom) {
customPainter?.call(
canvas: canvas,
size: size,
decoration: this,
paint: Paint()
..color = patternColor
..strokeWidth = strokeWidth,
xStart: xStart,
yStart: yStart,
width: patternWidth,
height: patternHeight,
);
return;
}
// Draw the background
final backgroundPaint = Paint()
..color = backgroundColor
..style = PaintingStyle.fill;
canvas.drawRect(
Rect.fromLTWH(0.0, 0.0, size.width, size.height),
backgroundPaint,
);
// If type is blank only the background is drawn
if (type == PencilDecorationType.blank) return;
// ySpacing will be calculated either based on number of lines or the
// spacing given.
late double ySpacing;
if (type == PencilDecorationType.ruled ||
type == PencilDecorationType.chequered ||
type == PencilDecorationType.dots) {
if (numberOfLines != null) {
ySpacing =
numberOfLines == 1 ? patternHeight : patternHeight / numberOfLines!;
}
if (spacing != null) {
ySpacing = spacing!;
}
}
// x spacing is the same as y spacing for the moment
final double xSpacing = ySpacing;
// Draw vertical lines.
if (type == PencilDecorationType.chequered) {
for (int column = 0; column <= patternWidth / xSpacing; column++) {
Paint? linePaint = Paint()
..color = patternColor
..strokeWidth = strokeWidth;
//..blendMode = BlendMode.srcOver;
if (paintProvider != null) {
linePaint = paintProvider?.call(column, -1, linePaint);
}
if (linePaint == null) continue;
canvas.drawLine(
Offset(xStart + xSpacing * column, padding.top),
Offset(xStart + xSpacing * column, padding.top + patternHeight),
linePaint,
);
}
}
// Draw horizontal lines
if (type == PencilDecorationType.ruled ||
type == PencilDecorationType.chequered) {
for (int row = 0; row <= patternHeight / ySpacing; row++) {
Paint? linePaint = Paint()
..color = patternColor
..strokeWidth = strokeWidth;
if (paintProvider != null) {
linePaint = paintProvider?.call(-1, row, linePaint);
}
if (linePaint == null) continue;
canvas.drawLine(
Offset(xStart, yStart + ySpacing * row),
Offset(xStart + patternWidth, yStart + ySpacing * row),
linePaint,
);
}
}
// Draw the dotted pattern
if (type == PencilDecorationType.dots) {
for (int column = 0; column <= patternWidth / xSpacing; column++) {
for (int row = 0; row <= patternHeight / ySpacing; row++) {
Paint? linePaint = Paint()
..color = patternColor
..strokeWidth = strokeWidth;
if (paintProvider != null) {
linePaint = paintProvider?.call(column, row, linePaint);
}
if (linePaint == null) continue;
canvas.drawCircle(
Offset(xStart + xSpacing * column, yStart + ySpacing * row),
strokeWidth / 2,
linePaint,
);
}
}
}
// Finally draw the frame
if (hasBorder) {
Paint? linePaint = Paint()
..color = patternColor
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke;
if (paintProvider != null) {
linePaint = paintProvider?.call(-1, -1, linePaint);
}
if (linePaint != null) {
canvas.drawRect(
Rect.fromLTWH(
xStart + linePaint.strokeWidth / 2,
yStart + linePaint.strokeWidth / 2,
patternWidth - linePaint.strokeWidth,
patternHeight - linePaint.strokeWidth,
),
linePaint,
);
}
}
}