Skip to content

Instantly share code, notes, and snippets.

@nhancv
Last active December 29, 2023 14:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhancv/3d6ac4b6b684ccc517270bf145b9b43b to your computer and use it in GitHub Desktop.
Save nhancv/3d6ac4b6b684ccc517270bf145b9b43b to your computer and use it in GitHub Desktop.
Flutter custom painter: rounded rect and arrow
class _MenuBoxBackground extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final Paint paint = Paint()
..color = Colors.white
..strokeWidth = 1.W
..style = PaintingStyle.fill;
final double triangleH = 10.H;
final double triangleW = 25.0.W;
final double width = size.width;
final double height = size.height;
final Path trianglePath = Path()
..moveTo(width / 2 - triangleW / 2, height)
..lineTo(width / 2, triangleH + height)
..lineTo(width / 2 + triangleW / 2, height)
..lineTo(width / 2 - triangleW / 2, height);
canvas.drawPath(trianglePath, paint);
final BorderRadius borderRadius = BorderRadius.circular(15.W);
final Rect rect = Rect.fromLTRB(0, 0, width, height);
final RRect outer = borderRadius.toRRect(rect);
canvas.drawRRect(outer, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => false;
}
CustomPaint(
painter: _MenuBoxBackground(),
child: Container(
padding: EdgeInsets.only(
left: 15.W, right: 15.W, bottom: 20.H, top: 20.H),
child: ...,
),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment