簡體   English   中英

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

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

如果我在 flutter 中繪制了一個自定義形狀,我如何在它周圍給它一個陰影(即給它高度)?

您可以使用Material()

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

Canvas上的drawShadow方法允許您指定elevation和陰影color

下面是一個高度為 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;
  }
}

結果:
在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM