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

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