它适用于某些试验,然后不适用于某些试验,然后再次工作(没有任何特定模式)。我的意思是,例如,它会校正线条并仅使用第一个和最后一个点绘制完美的线条 3-4 次,然后有一次它离开用户绘制的线条。似乎points.clear
orpoints.add
有时有效,有时无效。我知道这听起来很傻,但我无法用合乎逻辑的方式解释它。如果有人可以查看代码并告诉我发生了什么,我将非常感激。
这是我的手势检测器:
body: GestureDetector(
onPanDown: (DragDownDetails details) {
setState(() {
points.clear();
});
},
onPanUpdate: (DragUpdateDetails details) {
//when the user touch the screen and move
setState(() {
RenderBox box = context.findRenderObject(); //finds the scaffold
Offset point = box.globalToLocal(details.globalPosition);
point = point.translate(0.0, -(AppBar().preferredSize.height + 30));
points = List.from(points)
..add(point); //add the points when user drag in screen
firstPoint = points.first; //storing the first point for drawing the line
});
},
onPanEnd: (DragEndDetails details) {
setState(() {
if (mode == "Line") {
Offset lastPoint = points.last; //storing the last point for drawing the line
points.clear();
points.add(firstPoint);
points.add(lastPoint);
points.add(null);
}
});
},
child: sketchArea,
),
这是我的 CustomPainter 类:
class Sketcher2 extends CustomPainter {
final List<Offset> points;
Sketcher2(this.points);
@override
bool shouldRepaint(Sketcher2 oldDelegate) {
return oldDelegate.points != points;
}
void paint(Canvas canvas, Size size) {
Paint paint = Paint()
..color = Colors.black //seting the color of the drawing
..strokeCap = StrokeCap.round //the shape of a single dot (single touch)
..strokeWidth = 4.0; // the width of a single dot (single touch)
for (int i = 0; i < points.length - 1; i++) {
if (points[i] != null && points[i + 1] != null) {
canvas.drawLine(points[i], points[i + 1], paint);
}
}
}
}