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.

This is an extension of the ShapeBorder.paint method. It allows BoxBorder borders to be applied to different BoxShapes and with different borderRadius parameters, without changing the BoxBorder object itself.

The shape argument specifies the BoxShape to draw the border on.

If the shape is specifies a rectangular box shape (BoxShape.rectangle), then the borderRadius argument describes the corners of the rectangle.

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

See also:

  • paintBorder, which is used if the border has non-uniform colors or styles and no borderRadius.
  • Border.paint, similar to this method, includes additional comments and provides more details on each parameter than described here.

Implementation

@override
void paint(Canvas canvas, Rect rect,
    {TextDirection? textDirection,
    BoxShape shape = BoxShape.rectangle,
    BorderRadius? borderRadius}) {
  orignPath.reset();
  sketchPath.reset();
  sketchPoints.clear();
  switch (shape) {
    case BoxShape.rectangle:
      if (borderRadius != null) {
        orignPath.addRRect(borderRadius.toRRect(rect));
      } else {
        orignPath.addRect(rect);
      }
      break;
    case BoxShape.circle:
      orignPath.addOval(rect);
      break;
  }
  PathMetrics pathMetrics = orignPath.computeMetrics();
  // y = .02 + 2
  //100 4   300 8
  double dashWidth = this.dashWidth;
  if (dashWidth == 1) {
    var def = rect.longestSide * .02 + 2;
    dashWidth = def;
  }
  dashWidth = math.max(dashWidth, 3);
  double distance = 0.0;
  Offset? start;
  Offset? middle;

  LineParse line = LineParse(dashWidth);
  while (pathMetrics.iterator.moveNext()) {
    PathMetric pathMetric = pathMetrics.iterator.current;
    while (distance < pathMetric.length) {
      var extractPath =
          pathMetric.extractPath(distance, distance += dashWidth);
      Rect bound = extractPath.getBounds();

      distance += dashWidth;
      // canvas.drawRect(bound, _paint2..style = PaintingStyle.stroke);
      // canvas.drawCircle(bound.center, 1, _paint..style = PaintingStyle.fill);
      Offset next = bound.center;
      if (middle == null && start == null) {
        start = next;
        continue;
      }
      if (middle == null) {
        middle = next;
      } else {
        line.update(start!, next, rect.bottomRight);
        line.parse(sketchPoints);
        start = middle;
        middle = next;
      }
    }
  }

  for (var element in sketchPoints) {
    sketchPath.moveTo(element.start.dx, element.start.dy);
    sketchPath.quadraticBezierTo(element.control.dx, element.control.dy,
        element.end.dx, element.end.dy);
  }
  canvas.drawPath(sketchPath, _paint);
}