Created
April 1, 2024 06:27
-
-
Save BarryDaBee/3dc4f005392d2b6f918d95e4a648ad48 to your computer and use it in GitHub Desktop.
Trapezoid shape with rounded corners
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:math' as math; | |
class RoundedTrapezoidShapePainter extends CustomPainter { | |
/// BackgroundColor of the shape | |
/// | |
final Color? color; | |
/// Radius of the topRight and topLeft corners | |
/// | |
final double radius; | |
/// Subtracted from the height of the painter to get the height | |
/// of the short side. | |
/// | |
final double shortSideDifference; | |
RoundedTrapezoidShapePainter({ | |
required this.shortSideDifference, | |
required this.radius, | |
this.color, | |
}); | |
@override | |
void paint(Canvas canvas, Size size) { | |
final width = size.width; | |
final height = size.height; | |
final paint = Paint() | |
..color = color ?? Colors.transparent | |
..strokeWidth = 1 | |
..style = PaintingStyle.fill; | |
final path = Path() | |
..arcTo(Rect.fromLTWH(0, 0, radius * 2, radius * 2), math.pi, math.pi / 2, | |
false) | |
..arcTo(Rect.fromLTWH(width - radius * 2, 0, radius * 2, radius * 2), | |
-math.pi / 2, math.pi / 2, false) | |
..lineTo(width, height) | |
..lineTo(0, height - shortSideDifference); | |
canvas.drawPath(path, paint); | |
} | |
@override | |
bool shouldRepaint(RoundedTrapezoidShapePainter oldDelegate) { | |
return color != oldDelegate.color; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment