简体   繁体   English

flutter 中的 CustomPaint 小部件的高程属性是否有替代方法?

[英]Is there an alternative for an elevation property for CustomPaint widget in flutter?

If I have drawn a custom shape in flutter, how do I give it a shadow around it (ie give it elevation)?如果我在 flutter 中绘制了一个自定义形状,我如何在它周围给它一个阴影(即给它高度)?

You can make use of Material()您可以使用Material()

return Material(
       elevation: 20,
       child CustomShape(),
       );

The drawShadow method on Canvas lets you specify the elevation and the shadow color . Canvas上的drawShadow方法允许您指定elevation和阴影color

Below is an example of a pink rounded rectangle with elevation 10 and grey shadow.下面是一个高度为 10 和灰色阴影的粉红色圆角矩形的示例。

class MyPainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    canvas.save();

    final shape = RRect.fromRectAndRadius(
      Rect.fromLTWH(0, 0, 200, 100),
      Radius.circular(3),
    );

    final path = Path()..addRRect(shape);

    // Draw the shadow before the path.
    // shadow color: grey
    // elevation: 10
    // opaque object
    canvas.drawShadow(path, Colors.grey, 10, false);
    canvas.drawPath(path, Paint()..color = Colors.pink);

    canvas.restore();
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return false;
  }
}

Result:结果:
在此处输入图像描述

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

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