|
| 1 | +import 'dart:math' as math; |
| 2 | + |
| 3 | +import 'package:flutter/widgets.dart'; |
| 4 | + |
| 5 | +/// Clip widget in star shape |
| 6 | +class WavyCircleClipper extends CustomClipper<Path> { |
| 7 | + WavyCircleClipper(this.numberOfPoints); |
| 8 | + |
| 9 | + /// The number of points of the star |
| 10 | + final int numberOfPoints; |
| 11 | + |
| 12 | + late int counter = 0; |
| 13 | + |
| 14 | + @override |
| 15 | + Path getClip(Size size) { |
| 16 | + double width = size.width; |
| 17 | + |
| 18 | + double halfWidth = width / 2; |
| 19 | + |
| 20 | + double radius = halfWidth / 1.11; |
| 21 | + |
| 22 | + double outerCurveRadius = width / 2.08; |
| 23 | + |
| 24 | + double innerCurveRadius = width / 2.42; |
| 25 | + |
| 26 | + double degreesPerStep = _degToRad(360 / numberOfPoints) as double; |
| 27 | + |
| 28 | + double halfDegreesPerStep = degreesPerStep / 2; |
| 29 | + |
| 30 | + var path = Path(); |
| 31 | + |
| 32 | + double max = 2 * math.pi; |
| 33 | + |
| 34 | + path.moveTo(width, halfWidth); |
| 35 | + |
| 36 | + path.moveTo( |
| 37 | + halfWidth + radius * math.cos(0 + halfDegreesPerStep), |
| 38 | + halfWidth + radius * math.sin(0 + halfDegreesPerStep), |
| 39 | + ); |
| 40 | + |
| 41 | + for (double step = 0; step < max + 1; step += degreesPerStep) { |
| 42 | + if (counter % 2 == 0) { |
| 43 | + path.quadraticBezierTo( |
| 44 | + halfWidth + outerCurveRadius * math.cos(step), |
| 45 | + halfWidth + outerCurveRadius * math.sin(step), |
| 46 | + halfWidth + radius * math.cos(step + halfDegreesPerStep), |
| 47 | + halfWidth + radius * math.sin(step + halfDegreesPerStep), |
| 48 | + ); |
| 49 | + } else { |
| 50 | + path.quadraticBezierTo( |
| 51 | + halfWidth + innerCurveRadius * math.cos(step), |
| 52 | + halfWidth + innerCurveRadius * math.sin(step), |
| 53 | + halfWidth + radius * math.cos(step + halfDegreesPerStep), |
| 54 | + halfWidth + radius * math.sin(step + halfDegreesPerStep), |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + counter++; |
| 59 | + } |
| 60 | + path.close(); |
| 61 | + return path; |
| 62 | + } |
| 63 | + |
| 64 | + num _degToRad(num deg) => deg * (math.pi / 180.0); |
| 65 | + |
| 66 | + @override |
| 67 | + bool shouldReclip(CustomClipper<Path> oldClipper) { |
| 68 | + WavyCircleClipper oldie = oldClipper as WavyCircleClipper; |
| 69 | + return numberOfPoints != oldie.numberOfPoints; |
| 70 | + } |
| 71 | +} |
0 commit comments