paint method
Paints the border within the given Rect on the given Canvas.
The textDirection
argument must be provided and non-null if the border
has a text direction dependency (for example if it is expressed in terms
of "start" and "end" instead of "left" and "right"). It may be null if
the border will not need the text direction to paint itself.
Implementation
@override
void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) {
final width = rect.width;
final borderWidthSize = width / 2;
final height = rect.height;
final borderOffset = borderWidth / 2;
final bLength =
borderLength > min(cutOutHeight, cutOutHeight) / 2 + borderWidth * 2
? borderWidthSize / 2
: borderLength;
final cutWidth = cutOutWidth < width ? cutOutWidth : width - borderOffset;
final cutHeight =
cutOutHeight < height ? cutOutHeight : height - borderOffset;
final backgroundPaint = Paint()
..color = overlayColor
..style = PaintingStyle.fill;
final borderPaint = Paint()
..color = borderColor
..style = PaintingStyle.stroke
..strokeWidth = borderWidth;
final boxPaint = Paint()
..color = borderColor
..style = PaintingStyle.fill
..blendMode = BlendMode.dstOut;
final cutOutRect = Rect.fromLTWH(
rect.left + width / 2 - cutWidth / 2 + borderOffset,
-cutOutBottomOffset +
rect.top +
height / 2 -
cutHeight / 2 +
borderOffset,
cutWidth - borderOffset * 2,
cutHeight - borderOffset * 2,
);
canvas
..saveLayer(
rect,
backgroundPaint,
)
..drawRect(
rect,
backgroundPaint,
)
/// Draw top right corner
..drawRRect(
RRect.fromLTRBAndCorners(
cutOutRect.right - bLength,
cutOutRect.top,
cutOutRect.right,
cutOutRect.top + bLength,
topRight: Radius.circular(borderRadius),
),
borderPaint,
)
/// Draw top left corner
..drawRRect(
RRect.fromLTRBAndCorners(
cutOutRect.left,
cutOutRect.top,
cutOutRect.left + bLength,
cutOutRect.top + bLength,
topLeft: Radius.circular(borderRadius),
),
borderPaint,
)
/// Draw bottom right corner
..drawRRect(
RRect.fromLTRBAndCorners(
cutOutRect.right - bLength,
cutOutRect.bottom - bLength,
cutOutRect.right,
cutOutRect.bottom,
bottomRight: Radius.circular(borderRadius),
),
borderPaint,
)
/// Draw bottom left corner
..drawRRect(
RRect.fromLTRBAndCorners(
cutOutRect.left,
cutOutRect.bottom - bLength,
cutOutRect.left + bLength,
cutOutRect.bottom,
bottomLeft: Radius.circular(borderRadius),
),
borderPaint,
)
..drawRRect(
RRect.fromRectAndRadius(
cutOutRect,
Radius.circular(borderRadius),
),
boxPaint,
)
..restore();
}