getOuterPath method

  1. @override
Path getOuterPath(
  1. Rect host,
  2. Rect? guest
)
override

Creates a Path that describes the outline of the shape.

The host is the bounding rectangle of the shape.

The guest is the bounding rectangle of the shape for which a notch will be made. It is null when there is no guest.

Implementation

@override
Path getOuterPath(Rect host, Rect? guest) {
  if (guest == null || !host.overlaps(guest)) return Path()..addRect(host);

  double notchRadius = guest.width / 2.0;

  final double s1 = notchSmoothness.s1;
  final double s2 = notchSmoothness.s2;

  double r = notchRadius;
  double a = -1.0 * r - s2;
  double b = host.top - guest.center.dy;

  double n2 = math.sqrt(b * b * r * r * (a * a + b * b - r * r));
  double p2xA = ((a * r * r) - n2) / (a * a + b * b);
  double p2xB = ((a * r * r) + n2) / (a * a + b * b);
  double p2yA = convexBridge ? -math.sqrt(r * r - p2xA * p2xA) : math.sqrt(r * r - p2xA * p2xA);
  double p2yB = convexBridge ? -math.sqrt(r * r - p2xB * p2xB) : math.sqrt(r * r - p2xB * p2xB);

  List<Offset> p = List.filled(6, Offset.zero, growable: true);

  // p0, p1, and p2 are the control points for segment A.
  p[0] = Offset(a - s1, b);
  p[1] = Offset(a, b);
  double cmp = b < 0 ? -1.0 : 1.0;
  p[2] = cmp * p2yA > cmp * p2yB ? Offset(p2xA, p2yA) : Offset(p2xB, p2yB);

  // p3, p4, and p5 are the control points for segment B, which is a mirror
  // of segment A around the y axis.
  p[3] = Offset(-1.0 * p[2].dx, p[2].dy);
  p[4] = Offset(-1.0 * p[1].dx, p[1].dy);
  p[5] = Offset(-1.0 * p[0].dx, p[0].dy);

  // translate all points back to the absolute coordinate system.
  for (int i = 0; i < p.length; i += 1) {
    p[i] = p[i] + guest.center;
  }

  double leftCornerRadius = this.leftCornerRadius * (animation?.value ?? 1);
  double rightCornerRadius = this.rightCornerRadius * (animation?.value ?? 1);
  if (isHexagon) {
    return Path()
      ..moveTo(host.left, host.top)
      ..lineTo(host.left, host.top + leftCornerRadius)
      ..arcToPoint(
        Offset(host.left + leftCornerRadius, host.top),
        radius: Radius.circular(leftCornerRadius),
        clockwise: true,
      )
      ..lineTo(p[0].dx, p[0].dy)
      ..lineTo(p[1].dx, p[1].dy)
      ..lineTo(p[4].dx, p[4].dy)
      ..lineTo(host.right, host.top)
      ..lineTo(host.right - rightCornerRadius, host.top)
      ..arcToPoint(
        Offset(host.right, host.top + rightCornerRadius),
        radius: Radius.circular(rightCornerRadius),
        clockwise: true,
      )
      ..lineTo(host.right, host.bottom)
      ..lineTo(host.left, host.bottom)
      ..moveTo(p[0].dx + 20, p[0].dy)
      ..conicTo(p[1].dx + 41, p[1].dy - 23, p[4].dx - 5, p[4].dy, 10)
      ..close();
  }
  if (drawHexagon) {
    return Path()
      ..moveTo(host.left, host.top)
      ..lineTo(host.left, host.bottom)
      ..lineTo(host.left, host.top + leftCornerRadius)
      ..arcToPoint(
        Offset(host.left + leftCornerRadius, host.top),
        radius: Radius.circular(leftCornerRadius),
        clockwise: true,
      )
      ..lineTo(p[2].dx + 4, host.top)
      ..conicTo(p[2].dx + 4, 2 * p[2].dy * 5.2 / 4, p[3].dx - 36, p[3].dy * 4.6, 6)
      ..quadraticBezierTo(p[3].dx - 38, p[3].dy * 4.6, p[3].dx - 32, p[3].dy * 4.6)
      ..conicTo(p[4].dx - 6.5, 2 * p[2].dy * 5.2 / 4, p[5].dx - 22, p[5].dy, 6)
      ..lineTo(host.right - rightCornerRadius, host.top)
      ..arcToPoint(
        Offset(host.right, host.top + rightCornerRadius),
        radius: Radius.circular(rightCornerRadius),
        clockwise: true,
      )
      ..lineTo(host.right, host.top)
      ..lineTo(host.right, host.bottom)
      ..lineTo(host.left, host.bottom)
      ..close();
  }
  if (convexBridge) {
    return Path()
      ..moveTo(host.left, host.top)
      ..lineTo(host.left, host.top + leftCornerRadius)
      ..arcToPoint(
        Offset(host.left + leftCornerRadius, host.top),
        radius: Radius.circular(leftCornerRadius),
        clockwise: true,
      )
      ..lineTo(p[0].dx, p[0].dy)
      ..quadraticBezierTo(p[1].dx, p[1].dy, p[2].dx, p[2].dy)
      ..arcToPoint(
        p[3],
        radius: Radius.circular(notchRadius),
        clockwise: true,
      )
      ..quadraticBezierTo(p[4].dx, p[4].dy, p[5].dx, p[5].dy)
      ..lineTo(host.right - rightCornerRadius, host.top)
      ..arcToPoint(
        Offset(host.right, host.top + rightCornerRadius),
        radius: Radius.circular(rightCornerRadius),
        clockwise: true,
      )
      ..lineTo(host.right, host.top)
      ..lineTo(host.right, host.bottom)
      ..lineTo(host.left, host.bottom)
      ..close();
  }
  return Path()
    ..moveTo(host.left, host.bottom)
    ..lineTo(host.left, host.top)
    ..lineTo(host.left, host.top + leftCornerRadius)
    ..arcToPoint(
      Offset(host.left + leftCornerRadius, host.top),
      radius: Radius.circular(leftCornerRadius),
      clockwise: true,
    )
    ..lineTo(p[0].dx, p[0].dy)
    ..quadraticBezierTo(p[1].dx, p[1].dy, p[2].dx, p[2].dy)
    ..arcToPoint(
      p[3],
      radius: Radius.circular(notchRadius - 1),
      clockwise: false,
    )
    ..quadraticBezierTo(p[4].dx, p[4].dy, p[5].dx, p[5].dy)
    ..lineTo(host.right - rightCornerRadius, host.top)
    ..arcToPoint(
      Offset(host.right, host.top + rightCornerRadius),
      radius: Radius.circular(rightCornerRadius),
      clockwise: true,
    )
    ..lineTo(host.right, host.top)
    ..lineTo(host.right, host.bottom)
    ..lineTo(host.left, host.bottom)
    ..close();
}