centered method

void centered(
  1. void paint(
    1. PCanvas pCanvas,
    2. Point point,
    3. PDimension size
    ), {
  2. num? x,
  3. num? y,
  4. Point? point,
  5. PDimension? area,
  6. num? width,
  7. num? height,
  8. PDimension? dimension,
  9. PDimension sizer()?,
  10. double? scale,
})

A helper funtion to center draw operations.

Implementation

void centered(
    void Function(PCanvas pCanvas, Point point, PDimension size) paint,
    {num? x,
    num? y,
    Point? point,
    PDimension? area,
    num? width,
    num? height,
    PDimension? dimension,
    PDimension Function()? sizer,
    double? scale}) {
  if (x == null || y == null) {
    if (point == null) {
      if (area == null) {
        throw ArgumentError("Parameters `point` and `area` not provided!");
      }
      point = area.center;
    }

    x ??= point.x;
    y ??= point.y;
  }

  if (width == null || height == null) {
    if (dimension == null) {
      if (sizer == null) {
        throw ArgumentError(
            "Parameters `dimension` and `sizer` not provided!");
      }
      dimension = sizer();
    }

    if (dimension is PTextMetric) {
      width ??= dimension.actualWidth.toInt();
      height ??= dimension.actualHeight.toInt();
    } else {
      width ??= dimension.width.toInt();
      height ??= dimension.height.toInt();
    }
  }

  if (scale != null) {
    width = (width * scale).toInt();
    height = (height * scale).toInt();
  }

  var x2 = x - (width ~/ 2);
  var y2 = y - (height ~/ 2);

  paint(this, Point(x2, y2), PDimension(width, height));
}