【发布时间】:2019-09-02 12:07:59
【问题描述】:
我想知道是否有人对如何开始使用 CustomPainter 绘制心形有任何指示。我已经设法画出三角形和正方形或基本圆形之类的东西,但心脏当然有直线和曲线。
我有这个画一个三角形,看起来有点像心脏,但不知道如何获得心脏所需的曲线。
class Heart extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: CustomPaint(
painter: TrianglePainter(
strokeColor: Color(0xFFF27788),
paintingStyle: PaintingStyle.fill,
),
child: Container(
height: 60 * Dep.hr,
width: 60 * Dep.hr,
),
),
);
}
}
class TrianglePainter extends CustomPainter {
final Color strokeColor;
final PaintingStyle paintingStyle;
final double strokeWidth;
TrianglePainter({this.strokeColor, this.strokeWidth = 3, this.paintingStyle = PaintingStyle.stroke});
@override
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = strokeColor
..strokeWidth = strokeWidth
..style = paintingStyle;
canvas.drawPath(getTrianglePath(size.width, size.height), paint);
}
Path getTrianglePath(double x, double y) {
return Path()
..moveTo(y, 0)
..lineTo(0, 0)
..lineTo(x / 2, y);
}
@override
bool shouldRepaint(TrianglePainter oldDelegate) {
return oldDelegate.strokeColor != strokeColor ||
oldDelegate.paintingStyle != paintingStyle ||
oldDelegate.strokeWidth != strokeWidth;
}
}
而且它只是一块颜色,但我也确实需要在形状周围加上边框。这是我的预期输出,不知道是不是一厢情愿。
【问题讨论】:
-
使用 drawArc() 函数在 CustomPainter 中绘制曲线。
-
什么是 Dep.hr ??
标签: flutter dart custom-painting