简体   繁体   English

如何在 flutter 中圆化 canvas 的角

[英]How to round the corners of canvas in flutter

I am looking to round the corners of an image drawn on the canvas.我正在寻找在 canvas 上绘制的图像的角落。

My end result should look like this below image.我的最终结果应该如下图所示。 An image with rounded corners and drop shadow, I have managed to add the drop shadow.带有圆角和阴影的图像,我设法添加了阴影。

目标图像

Here's my painter code so far.到目前为止,这是我的画家代码。

class ImagePainter extends CustomPainter {
  final ui.Image image;

  const ImagePainter(this.image);

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint();
    double shadowBlurRadius = 22.0;
    Color shadowColor = Colors.black38;

    //Adds a Drop Shadow
    canvas.drawPath(
      Path()
        ..addRect(Rect.fromPoints(
            Offset(0, 0), Offset(size.width + 15, size.height + 15))),
      Paint()
        ..color = shadowColor
        ..maskFilter = MaskFilter.blur(
            BlurStyle.normal, (shadowBlurRadius * 0.57735 + 0.5)),
    );

    //Draw Image
    canvas.drawImage(image, Offset.zero, paint);
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) => false;
}

Try wrapping your canvas in ClipRRect .尝试将 canvas 包装在ClipRRect中。

ClipRRect(
    borderRadius: BorderRadius.circular(10),
    child: //your canvas
    ),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM