paint method

  1. @override
void paint(
  1. Canvas canvas,
  2. Rect rect, {
  3. TextDirection? textDirection,
})
override

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 double width = rect.width;
  final double height = rect.height;
  final double borderOffset = borderWidth / 2;
  final double newBorderLength = borderLength;
  final double newCutOutSize = width * cutOutSize;

  final Paint backgroundPaint = Paint()
    ..color = overlayColor
    ..style = PaintingStyle.fill;

  final Paint borderPaint = Paint()
    ..color = borderColor
    ..style = PaintingStyle.stroke
    ..strokeWidth = borderWidth;

  final Paint boxPaint = Paint()
    ..color = borderColor
    ..style = PaintingStyle.fill
    ..blendMode = BlendMode.dstOut;

  final Rect cutOutRect = Rect.fromLTWH(
    rect.left + width / 2 - newCutOutSize / 2 + borderOffset,
    rect.top + height / 2 - newCutOutSize / 2 + borderOffset,
    newCutOutSize - borderOffset * 2,
    newCutOutSize - borderOffset * 2,
  );

  canvas
    ..saveLayer(
      rect,
      backgroundPaint,
    )
    ..drawRect(
      rect,
      backgroundPaint,
    )
    // Draw top right corner
    ..drawRRect(
      RRect.fromLTRBAndCorners(
        cutOutRect.right - newBorderLength,
        cutOutRect.top,
        cutOutRect.right,
        cutOutRect.top + newBorderLength,
        topRight: Radius.circular(borderRadius),
      ),
      borderPaint,
    )
    // Draw top left corner
    ..drawRRect(
      RRect.fromLTRBAndCorners(
        cutOutRect.left,
        cutOutRect.top,
        cutOutRect.left + newBorderLength,
        cutOutRect.top + newBorderLength,
        topLeft: Radius.circular(borderRadius),
      ),
      borderPaint,
    )
    // Draw bottom right corner
    ..drawRRect(
      RRect.fromLTRBAndCorners(
        cutOutRect.right - newBorderLength,
        cutOutRect.bottom - newBorderLength,
        cutOutRect.right,
        cutOutRect.bottom,
        bottomRight: Radius.circular(borderRadius),
      ),
      borderPaint,
    )
    // Draw bottom left corner
    ..drawRRect(
      RRect.fromLTRBAndCorners(
        cutOutRect.left,
        cutOutRect.bottom - newBorderLength,
        cutOutRect.left + newBorderLength,
        cutOutRect.bottom,
        bottomLeft: Radius.circular(borderRadius),
      ),
      borderPaint,
    )
    ..drawRRect(
      RRect.fromRectAndRadius(
        cutOutRect,
        Radius.circular(borderRadius),
      ),
      boxPaint,
    )
    ..restore();
}