您可以使用Path.combine https://api.flutter.dev/flutter/dart-ui/Path/combine.html随着difference https://api.flutter.dev/flutter/dart-ui/PathOperation.html#difference创建孔的操作。
定制画家:
class HolePainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
final paint = Paint();
paint.color = Colors.blue;
canvas.drawPath(
Path.combine(
PathOperation.difference,
Path()..addRRect(RRect.fromLTRBR(100, 100, 300, 300, Radius.circular(10))),
Path()
..addOval(Rect.fromCircle(center: Offset(200, 200), radius: 50))
..close(),
),
paint,
);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return null;
}
}
Usage :
class EditAvatar extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Hole in rounded rectangle'),
),
body: CustomPaint(
painter: HolePainter(),
child: Container(),
),
}
}
Result :
当然,如果您希望孔成为圆角矩形,只需减去RRect
代替Circle
.