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