paint method

  1. @override
void paint(
  1. Canvas canvas,
  2. Rect rect, {
  3. TextDirection? textDirection,
  4. BoxShape shape = BoxShape.rectangle,
  5. BorderRadius? borderRadius,
})
override

Paints the border within the given Rect on the given Canvas.

Uniform borders and non-uniform borders with similar colors and styles are more efficient to paint than more complex borders.

You can provide a BoxShape to draw the border on. If the shape in BoxShape.circle, there is the requirement that the border has uniform color and style.

If you specify a rectangular box shape (BoxShape.rectangle), then you may specify a BorderRadius. If a borderRadius is specified, there is the requirement that the border has uniform color and style.

The getInnerPath and getOuterPath methods do not know about the shape and borderRadius arguments.

The textDirection argument is not used by this paint method.

See also:

  • paintBorder, which is used if the border has non-uniform colors or styles and no borderRadius.
  • pub.dev/packages/non_uniform_border, a package that implements a Non-Uniform Border on ShapeBorder, which is used by Material Design buttons and other widgets, under the "shape" field.

Implementation

@override
void paint(
    Canvas canvas,
    Rect rect, {
      TextDirection? textDirection,
      BoxShape shape = BoxShape.rectangle,
      BorderRadius? borderRadius,
    }) {

  Path getDashedPath({
    required math.Point<double> a,
    required math.Point<double> b,
    required gap,
  }) {
    final Size size = Size(b.x - a.x, b.y - a.y);
    final Path path = Path();
    path.moveTo(a.x, a.y);
    bool shouldDraw = true;
    math.Point<double> currentPoint = math.Point<double>(a.x, a.y);

    final num radians = math.atan(size.height / size.width);

    final num dx = math.cos(radians) * gap < 0
        ? math.cos(radians) * gap * -1
        : math.cos(radians) * gap;

    final num dy = math.sin(radians) * gap < 0
        ? math.sin(radians) * gap * -1
        : math.sin(radians) * gap;

    while (currentPoint.x <= b.x && currentPoint.y <= b.y) {
      shouldDraw
          ? path.lineTo(currentPoint.x, currentPoint.y)
          : path.moveTo(currentPoint.x, currentPoint.y);
      shouldDraw = !shouldDraw;
      currentPoint = math.Point(
        currentPoint.x + dx,
        currentPoint.y + dy,
      );
    }
    return path;
  }
  if (isUniform) {
    final Paint dashedPaint = Paint()
      ..color = dashColor
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;
    // top line
    final Path _topPath = getDashedPath(
      a: math.Point(rect.topLeft.dx, rect.topLeft.dy),
      b: math.Point(rect.topRight.dx, rect.topRight.dy),
      gap: gap,
    );
    // right line
    final Path _rightPath = getDashedPath(
      a: math.Point(rect.topRight.dx, rect.topRight.dy),
      b: math.Point(rect.bottomRight.dx, rect.bottomRight.dy),
      gap: gap,
    );
    // bottom line
    final Path _bottomPath = getDashedPath(
      a: math.Point(rect.bottomLeft.dx, rect.bottomLeft.dy),
      b: math.Point(rect.bottomRight.dx, rect.bottomRight.dy),
      gap: gap,
    );
    // left line
    final Path _leftPath = getDashedPath(
      a: math.Point(rect.topLeft.dx, rect.topLeft.dy),
      b: math.Point(rect.bottomLeft.dx, rect.bottomLeft.dy),
      gap: gap,
    );

    canvas.drawPath(_topPath, dashedPaint);
    canvas.drawPath(_rightPath, dashedPaint);
    canvas.drawPath(_bottomPath, dashedPaint);
    canvas.drawPath(_leftPath, dashedPaint);
  }

  paintBorder(canvas, rect, top: top, right: right, bottom: bottom, left: left);
}