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 EdgeInsets insets = dimensions.resolve(textDirection);
  final bool rtl = textDirection == TextDirection.rtl;

  final Path path = Path();
  final Paint paint = Paint()
    ..strokeWidth = 0.0;

  void drawEdge(Rect rect, Color color) {
    paint.color = color;
    path.reset();
    path.moveTo(rect.left, rect.top);
    if (rect.width == 0.0) {
      paint.style = PaintingStyle.stroke;
      path.lineTo(rect.left, rect.bottom);
    } else if (rect.height == 0.0) {
      paint.style = PaintingStyle.stroke;
      path.lineTo(rect.right, rect.top);
    } else {
      paint.style = PaintingStyle.fill;
      path.lineTo(rect.right, rect.top);
      path.lineTo(rect.right, rect.bottom);
      path.lineTo(rect.left, rect.bottom);
    }
    canvas.drawPath(path, paint);
  }

  if (start != null && start!.size != 0.0 && side.style != BorderStyle.none) {
    final Rect insetRect = Rect.fromLTWH(rect.left, rect.top + insets.top, rect.width, rect.height - insets.vertical);
    final double x = rtl ? rect.right - insets.right : rect.left;
    final double width = rtl ? insets.right : insets.left;
    final double height = insetRect.height * start!.size;
    final double y = (insetRect.height - height) * ((start!.alignment + 1.0) / 2.0);
    final Rect r = Rect.fromLTWH(x, y, width, height);
    drawEdge(r, side.color);
  }

  if (end != null && end!.size != 0.0 && side.style != BorderStyle.none) {
    final Rect insetRect = Rect.fromLTWH(rect.left, rect.top + insets.top, rect.width, rect.height - insets.vertical);
    final double x = rtl ? rect.left : rect.right - insets.right;
    final double width = rtl ? insets.left : insets.right;
    final double height = insetRect.height * end!.size;
    final double y = (insetRect.height - height) * ((end!.alignment + 1.0) / 2.0);
    final Rect r = Rect.fromLTWH(x, y, width, height);
    drawEdge(r, side.color);
  }

  if (top != null && top!.size != 0.0 && side.style != BorderStyle.none) {
    final double width = rect.width * top!.size;
    final double startX = (rect.width - width) * ((top!.alignment + 1.0) / 2.0);
    final double x = rtl ? rect.width - startX - width : startX;
    final Rect r = Rect.fromLTWH(x, rect.top, width, insets.top);
    drawEdge(r, side.color);
  }

  if (bottom != null && bottom!.size != 0.0 && side.style != BorderStyle.none) {
    final double width = rect.width * bottom!.size;
    final double startX = (rect.width - width) * ((bottom!.alignment + 1.0) / 2.0);
    final double x = rtl ? rect.width - startX - width: startX;
    final Rect r = Rect.fromLTWH(x, rect.bottom - insets.bottom, width, side.width);
    drawEdge(r, side.color);
  }
}