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