Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
684 views
in Technique[技术] by (71.8m points)

Flutter: How to draw rounded rectangle with cutout CustomPainter

I'm trying to create the rounded rectangle shape with the cutout from the following picture:

menu

I'm using that as the base for my bottom nav menu. I'm not super savvy with the CutomPainter and have been unable to get something that I'm happy with. This is the closest I've come.

final backgroundPaint = Paint()..color = AppColors.BLUE_GREY;
final cutoutPaint = Paint()..color = Colors.grey;

final shapeBounds = Rect.fromLTWH(0, 0, size.width, size.height);

final centerAvatar = Offset(shapeBounds.center.dx, shapeBounds.top);

final avatarBounds = Rect.fromCircle(center: centerAvatar, radius: 40);

final backgroundPath = Path()
  ..moveTo(0, size.height / 2)
  ..addRRect(
      RRect.fromLTRBR(0, 0, size.width, size.height, Radius.circular(25)))
  ..close();
    
final cutoutPath = Path()
  ..moveTo((size.width / 2) - 20, 0)
  ..arcTo(avatarBounds, pi, -pi, false);

canvas.drawPath(backgroundPath, backgroundPaint);
canvas.drawPath(cutoutPath, cutoutPaint);

Which gives me something more like this:

enter image description here

I don't know if I should be trying to draw the whole thing using a single path instead? I started with a rounded rectangle and then just tried to add the cutout on top. I did experiment with just making it a single path and using an arc for the cutout, but I was not able to get that along with the rounded corners to work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...