RoundedPolygon.rectangle constructor

RoundedPolygon.rectangle({
  1. double width = 2.0,
  2. double height = 2.0,
  3. CornerRounding rounding = .unrounded,
  4. List<CornerRounding>? perVertexRounding,
  5. Offset center = .zero,
})

Creates a rectangle width wide and height high about center, with optional rounding at its four corners.

The default dimensions and center fit the shape into the 2x2 box around the origin, which will usually need normalized to fit the unit square a shape border expects.

rounding rounds all four corners the same way. perVertexRounding overrides it, and must have 4 elements when it is not null. The default leaves the corners sharp.

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

Implementation

factory rectangle({
  double width = 2.0,
  double height = 2.0,
  CornerRounding rounding = .unrounded,
  List<CornerRounding>? perVertexRounding,
  Offset center = .zero,
}) {
  if (width <= 0.0 || height <= 0.0) {
    throw ArgumentError("Rectangles must have positive width and height.");
  }

  final left = center.x - width / 2.0;
  final top = center.y - height / 2.0;
  final right = center.x + width / 2.0;
  final bottom = center.y + height / 2.0;

  return .fromVertices(
    [
      .new(right, bottom),
      .new(left, bottom),
      .new(left, top),
      .new(right, top),
    ],
    rounding: rounding,
    perVertexRounding: perVertexRounding,
    center: center,
  );
}