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 width = rect.width;
  final borderWidthSize = width / 2;
  final height = rect.height;
  final borderOffset = borderWidth / 2;
  final _borderLength =
      borderLength > min(cutOutHeight, cutOutHeight) / 2 + borderWidth * 2
          ? borderWidthSize / 2
          : borderLength;
  final _cutOutWidth =
      cutOutWidth < width ? cutOutWidth : width - borderOffset;
  final _cutOutHeight =
      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 - _cutOutWidth / 2 + borderOffset,
    -cutOutBottomOffset +
        rect.top +
        height / 2 -
        _cutOutHeight / 2 +
        borderOffset,
    _cutOutWidth - borderOffset * 2,
    _cutOutHeight - borderOffset * 2,
  );

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