【发布时间】:2018-05-24 21:24:58
【问题描述】:
我有一个简单的 CustomPaint/CustomPainter 可以绘制一段圆(代码如下)。我读到我不能使用 GestureDetector 因为它不是一个合适的小部件,那么获取输入的最佳方式是什么?
我将有一堆片段放在一起,所以我需要像素精确的触摸位置。
我想到的两种可能性:
- 将painter放在SizedBox中,获取触摸坐标,手动计算是否在路径内。但这会使大量代码加倍。
- 使用 Material 类和自定义 BorderShape。这会很方便,但对我来说似乎很老套。
我的自定义画家:
class _SegmentPainter extends CustomPainter {
static const offset = -pi/2;
double start;
double end;
double innerRadius;
double outerRadius;
Color color;
_SegmentPainter(this.start, this.end, {this.innerRadius = 0.0, this.outerRadius, this.color});
@override bool shouldRepaint(CustomPainter oldDelegate) => this == oldDelegate;
@override bool shouldRebuildSemantics(CustomPainter oldDelegate) => this == oldDelegate;
@override
void paint(Canvas canvas, Size size) {
Path path = new Path();
path.arcTo(Rect.fromCircle(center: new Offset(0.0, 0.0), radius: outerRadius), offset + start, end-start, true);
path.relativeLineTo(-cos(offset + end)*(outerRadius-innerRadius), -sin(offset + end)*(outerRadius-innerRadius));
path.arcTo(Rect.fromCircle(center: new Offset(0.0, 0.0), radius: innerRadius), offset + end, start-end, false);
path.close();
canvas.drawPath(path, new Paint()..color = color..style = PaintingStyle.fill);
}
}
【问题讨论】:
-
????查看可触摸库:github.com/nateshmbhat/touchable