Line data Source code
1 : import 'package:flutter/cupertino.dart'; 2 : import 'package:flutter/painting.dart'; 3 : import 'dart:math' as math; 4 : 5 : class ConvexNotchedRectangle extends NotchedShape { 6 1 : const ConvexNotchedRectangle(); 7 : 8 1 : @override 9 : Path getOuterPath(Rect host, Rect guest) { 10 1 : if (guest == null || !host.overlaps(guest)) return Path()..addRect(host); 11 : 12 : // The guest's shape is a circle bounded by the guest rectangle. 13 : // So the guest's radius is half the guest width. 14 2 : final double notchRadius = guest.width / 2.0; 15 : 16 : const double s1 = 15.0; 17 : const double s2 = 1.0; 18 : 19 : final double r = notchRadius; 20 3 : final double a = -1.0 * r - s2; 21 4 : final double b = host.top - guest.center.dy; 22 : 23 10 : final double n2 = math.sqrt(b * b * r * r * (a * a + b * b - r * r)); 24 7 : final double p2xA = ((a * r * r) - n2) / (a * a + b * b); 25 7 : final double p2xB = ((a * r * r) + n2) / (a * a + b * b); 26 5 : final double p2yA = -math.sqrt(r * r - p2xA * p2xA); 27 5 : final double p2yB = -math.sqrt(r * r - p2xB * p2xB); 28 : 29 1 : final List<Offset> p = List<Offset>(6); 30 : 31 : // p0, p1, and p2 are the control points for segment A. 32 3 : p[0] = Offset(a - s1, b); 33 2 : p[1] = Offset(a, b); 34 2 : final double cmp = b < 0 ? -1.0 : 1.0; 35 5 : p[2] = cmp * p2yA > cmp * p2yB ? Offset(p2xA, p2yA) : Offset(p2xB, p2yB); 36 : 37 : // p3, p4, and p5 are the control points for segment B, which is a mirror 38 : // of segment A around the y axis. 39 8 : p[3] = Offset(-1.0 * p[2].dx, p[2].dy); 40 8 : p[4] = Offset(-1.0 * p[1].dx, p[1].dy); 41 8 : p[5] = Offset(-1.0 * p[0].dx, p[0].dy); 42 : 43 : // translate all points back to the absolute coordinate system. 44 3 : for (int i = 0; i < p.length; i += 1) { 45 3 : p[i] += guest.center; 46 : //p[i] += padding; 47 : } 48 1 : return Path() 49 3 : ..moveTo(host.left, host.top) 50 5 : ..lineTo(p[0].dx, p[0].dy) 51 9 : ..quadraticBezierTo(p[1].dx, p[1].dy, p[2].dx, p[2].dy) 52 1 : ..arcToPoint( 53 1 : p[3], 54 1 : radius: Radius.circular(notchRadius), 55 : clockwise: true, 56 : ) 57 9 : ..quadraticBezierTo(p[4].dx, p[4].dy, p[5].dx, p[5].dy) 58 3 : ..lineTo(host.right, host.top) 59 3 : ..lineTo(host.right, host.bottom) 60 3 : ..lineTo(host.left, host.bottom) 61 1 : ..close(); 62 : } 63 : }