drawRoundedBar function

void drawRoundedBar(
  1. Canvas canvas,
  2. Rect rect, {
  3. required double radius,
  4. required Paint paint,
  5. bool roundTop = true,
  6. bool roundBottom = false,
})

Draws a bar/rod shape with optional corner rounding.

Implementation

void drawRoundedBar(
  Canvas canvas,
  Rect rect, {
  required double radius,
  required Paint paint,
  bool roundTop = true,
  bool roundBottom = false,
}) {
  if (rect.isEmpty) return;

  final maxR = rect.shortestSide / 2;
  final r = radius.clamp(0.0, maxR);
  if (r <= 0) {
    canvas.drawRect(rect, paint);
    return;
  }

  canvas.drawRRect(
    RRect.fromRectAndCorners(
      rect,
      topLeft: roundTop ? Radius.circular(r) : Radius.zero,
      topRight: roundTop ? Radius.circular(r) : Radius.zero,
      bottomLeft: roundBottom ? Radius.circular(r) : Radius.zero,
      bottomRight: roundBottom ? Radius.circular(r) : Radius.zero,
    ),
    paint,
  );
}