我遇到了同样的问题,但我通过测量两点之间的距离来解决它。
The rule of how to measure the distance between two points
// Convert a rule to the code
double distanceBetweenTwoPoints(double x1,double y1 ,double x2, double y2){
double x = x1 - x2;
x = x * x;
double y = y1 - y2;
y = y * y;
double result = x + y;
return sqrt(result);
}
首先,声明两个变量及其值
// These two variables are to save the previous points
var fingerPostionY = 0.0,fingerPostionX = 0.0;
然后在onPanUpdate方法里面,我取了两个点来计算它们之间的距离。之后我做了一个对比,如果距离大(比如50)那么手指很多,所以我忽略它,否则就只有一根手指在屏幕上。
onPanUpdate: (details) {
if (fingerPostionY < 1.0){
// assigen for the first time to compare
fingerPostionY = details.globalPosition.dy;
fingerPostionX = details.globalPosition.dx;
}else{
// they use a lot of fingers
double distance = distanceBetweenTwoPoints(details.globalPosition.dx,details.globalPosition.dy,
fingerPostionX,fingerPostionY);
// the distance between two fingers must be above 50
// to disable multi touch
if(distance > 50)
return;
// update to use it in the next comparison
fingerPostionY = details.globalPosition.dy;
fingerPostionX = details.globalPosition.dx;
}
// the code of drawing
setState(() {
RenderBox renderBox = context.findRenderObject();
points.add(TouchPoints(
points: renderBox.globalToLocal(details.globalPosition),
paint: Paint()
..strokeCap = strokeType
..isAntiAlias = true
..color = activeColor.withOpacity(opacity)
..strokeWidth = strokeWidth));
});
},
重要提示:
-
在 onPanEnd 方法中,你必须写下这一行,因为它
表示手指现在已经竖起来了
fingerPostionY = 0.0;
-
绘图代码中仍有一些性能问题尚未解决
编辑:
我使用 path 提高了性能。
你可以在 GitHub 上看到我的代码:
free painting on flutter