如何在裁剪后的图像上添加边框?

Fig*_*gör 3 clip-path flutter

我可以使用ClipPath裁剪图像,但是如何向裁剪后的图像添加边框,如下所示?

在此处输入图片说明

Rao*_*che 8

您可以这样解决:

  1. 创建一个CustomPainter具有相同Path的CustomClipper<Path>

范例:

Path path = Path();
path.lineTo(0.0, 0.0);
path.lineTo(size.width, 0.0);
path.lineTo(size.width, size.height * 0.8);
path.lineTo(0.0, size.height);
path.close();
Run Code Online (Sandbox Code Playgroud)
  1. 创建一个Paint对象并使用“描边绘画样式” 创建,它strokeWidth是您自定义边框的宽度

码

Paint paint = Paint()
  ..style = PaintingStyle.stroke
  ..strokeWidth=10.0
  ..color = Colors.black;
Run Code Online (Sandbox Code Playgroud)

最后画出这条路 canvas

canvas.drawPath(path, paint);
Run Code Online (Sandbox Code Playgroud)

您还需要确保这CustomPaint是您容器的子代

ClipPath(
          clipper: traingleclipper(),
          child: Container(
            color: Colors.white,
            child: CustomPaint(
              painter: ClipperBorderPainter(),
            ),
          ),
        )
Run Code Online (Sandbox Code Playgroud)

在我的示例中,这是结果:

在此处输入图片说明

也有另一种使用的Stack方式,您将使用Clipper创建图像,然后CustomPaint使用相同的Path

Stack(
          children: <Widget>[
            ClipPath(
              clipper: CustomClip(),
                child: Image.network(
              "http://www.delonghi.com/Global/recipes/multifry/pizza_fresca.jpg",
              width: double.infinity,
              height: 400.0,
                  fit: BoxFit.cover,
            )),
            CustomPaint(
              painter: BorderPainter(),
              child: Container(
                height: 400.0,
              ),
            ),
          ],
        ),
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 完整的例子