addSquircleRect function

void addSquircleRect(
  1. Path path,
  2. double width,
  3. double height,
  4. double cornerRadius, {
  5. double extension = SquircleDefaults.extension,
  6. bool enabled = true,
})

path 追加一个 squircle 圆角矩形。对应 Kotlin Path.addSquircleRect

width/height 为像素尺寸,非正则不追加任何内容。 cornerRadius 会被夹到短边的一半。 extension 控制圆角瓦片相对 cornerRadius 的倍数,被夹到 SquircleDefaults.extensionMin..SquircleDefaults.extensionMaxenabled 为 false 时退化为普通圆角矩形。

Implementation

void addSquircleRect(
  Path path,
  double width,
  double height,
  double cornerRadius, {
  double extension = SquircleDefaults.extension,
  bool enabled = true,
}) {
  if (width <= 0 || height <= 0) return;
  if (!enabled) {
    final radius = cornerRadius.clamp(0.0, math.min(width, height) * 0.5);
    if (radius <= 0) {
      path.addRect(Rect.fromLTWH(0, 0, width, height));
    } else {
      path.addRRect(RRect.fromRectAndRadius(
        Rect.fromLTWH(0, 0, width, height),
        Radius.circular(radius),
      ));
    }
    return;
  }
  final extClamped =
      extension.clamp(SquircleDefaults.extensionMin, SquircleDefaults.extensionMax);
  final halfMin = math.min(width, height) * 0.5;
  final tile = (cornerRadius * extClamped).clamp(0.0, halfMin);
  if (tile <= 0) {
    path.addRect(Rect.fromLTWH(0, 0, width, height));
    return;
  }
  final handle = tile * (1.0 - _kSquircleControl);
  path
    ..moveTo(tile, 0)
    ..lineTo(width - tile, 0)
    ..cubicTo(width - handle, 0, width, handle, width, tile)
    ..lineTo(width, height - tile)
    ..cubicTo(width, height - handle, width - handle, height, width - tile, height)
    ..lineTo(tile, height)
    ..cubicTo(handle, height, 0, height - handle, 0, height - tile)
    ..lineTo(0, tile)
    ..cubicTo(0, handle, handle, 0, tile, 0)
    ..close();
}