RoundedPolygon.pill constructor

RoundedPolygon.pill({
  1. double width = 2,
  2. double height = 1,
  3. double smoothing = 0,
  4. Offset center = Offset.zero,
})

Creates a pill about center, width wide and height high: a rectangle capped by a semicircle at either end of its longer dimension.

smoothing extends the curve from the circular arc of each cap towards the edge between the two caps. The default of 0 leaves the caps as circular arcs.

Throws ArgumentError if either width or height is not greater than 0.

Implementation

factory RoundedPolygon.pill({
  double width = 2,
  double height = 1,
  double smoothing = 0,
  Offset center = Offset.zero,
}) {
  if (width <= 0 || height <= 0) {
    throw ArgumentError('Pill shapes must have positive width and height.');
  }

  final double wHalf = width / 2;
  final double hHalf = height / 2;

  return RoundedPolygon.fromVertices(
    [
      Point(wHalf + center.x, hHalf + center.y),
      Point(-wHalf + center.x, hHalf + center.y),
      Point(-wHalf + center.x, -hHalf + center.y),
      Point(wHalf + center.x, -hHalf + center.y),
    ],
    rounding: CornerRounding(radius: math.min(wHalf, hHalf), smoothing: smoothing),
    center: center,
  );
}