0

This question is purely based on GestureDetector flutter.

For Example: In Application, GestureDetector class is implemented so multitouch is supported by default, now we need to disable the multitouch so what would be the best way to do it?. Otherwise in a drawing app using GestureDetector in flutter cause multi touch issue.

So how to disable multitouch in gesture detector?

Alex Sergeenko
  • 642
  • 5
  • 22
Manukumar S B
  • 128
  • 1
  • 11
  • Possible duplicate of [How to disable multi-touch in mobile application using flutter](https://stackoverflow.com/questions/51712287/how-to-disable-multi-touch-in-mobile-application-using-flutter) – diegoveloper Aug 28 '18 at 05:09
  • But answers are not related to this question. – Manukumar S B Aug 28 '18 at 05:45

1 Answers1

0

I faced the same problem but I solve it by measuring the distance between two points.

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);
}

First of all, declare two variables with their values

// These two variables are to save the previous points
var fingerPostionY = 0.0,fingerPostionX = 0.0;

Then inside the onPanUpdate method, I took two points to calculate the distance between them. After that, I made a comparison, if the distance was large (e.g. 50) then there are many fingers, so I ignore it otherwise it will be just one finger on the screen.

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));
        });
      },

IMPORTANT NOTES:

  1. Inside onPanEnd method, you must write this line, because it means the finger is up now

    fingerPostionY = 0.0;

  2. Still there is some performance issue not solved yet in the drawing code


EDIT:

I enhanced the performance by using path.

You can see my code on the GitHub: free painting on flutter

tshi_chan
  • 3
  • 3