1

so i have a GestureDetector where if i scale it will increase or decrease the values ,The values are latitude and longitude, Next if i pan it will again do the same for latitude and longitude,

Please note these values are the values of image overlay on flutter_map

if I use either onScaleUpdate or onPanUpdate it works fine, but if i use both i get the error

The following assertion was thrown building LayoutBuilder:
Incorrect GestureDetector arguments.

Having both a pan gesture recognizer and a scale gesture recognizer is redundant; scale is a superset of pan.

Just use the scale gesture recognizer.

I need the specific latitude and longitude values , there are other values that will use scale or offset but this is not what i want as eventually i will need to convert those to latitude and longitude.

in short, how can use scale and pan at the same time in my gesture detector

this is the code

Positioned(
    top: 250,
    right: 10,
    child: GestureDetector(
        onScaleUpdate: (details) {
            double scaleFactor = details.scale;
            if (scaleFactor > 1) {
                topL[chosenPlate] !.latitude += tolerance;
                botL[chosenPlate] !.latitude -= tolerance;
                botR[chosenPlate] !.latitude -= tolerance;
                topL[chosenPlate] !.longitude -= tolerance;
                botL[chosenPlate] !.longitude -= tolerance;
                botR[chosenPlate] !.longitude += tolerance;
            } else if (scaleFactor < 1) {
                topL[chosenPlate] !.latitude -= tolerance;
                topL[chosenPlate] !.longitude -= tolerance;
                botL[chosenPlate] !.latitude += tolerance;
                botL[chosenPlate] !.longitude -= tolerance;
                botR[chosenPlate] !.latitude += tolerance;
                botR[chosenPlate] !.longitude += tolerance;
            }
        },
        onPanUpdate: (details) {
            if (details.delta.dx < 0) {
                setState(() {
                    topL[chosenPlate] !.longitude -= tolerance;
                    botL[chosenPlate] !.longitude -= tolerance;
                    botR[chosenPlate] !.longitude -= tolerance;
                });
            } else if (details.delta.dx > 0) {
                setState(() {
                    topL[chosenPlate] !.longitude += tolerance;
                    botL[chosenPlate] !.longitude += tolerance;
                    botR[chosenPlate] !.longitude += tolerance;
                });
            } else if (details.delta.dy < 0) {
                setState(() {
                    topL[chosenPlate] !.latitude += tolerance;
                    botL[chosenPlate] !.latitude += tolerance;
                    botR[chosenPlate] !.latitude += tolerance;
                });
            } else if (details.delta.dy > 0) {
                setState(() {
                    topL[chosenPlate] !.latitude -= tolerance;
                    botL[chosenPlate] !.latitude -= tolerance;
                    botR[chosenPlate] !.latitude -= tolerance;
                });
            }
        },
        child: Container(
            height: 20. h,
            width: 30. w,
            color: Color.fromARGB(255, 233, 176, 176),
        ),
    ),
),
Pannam
  • 482
  • 1
  • 3
  • 16
  • as the error says you have to remove `onPanUpdate` and make your logic based on `onScaleUpdate` only – pskink Apr 17 '23 at 07:17
  • @pskink ofcourse! i could do that, if i choose to keep onPanUpdate i loose the ability to zoom in or out (scale) , if i choose to keep `onScaleUpdate` i loose the ability to pan left or right, i can choose only one to get rid of the error but i need all the functionality, hence the question. – Pannam Apr 17 '23 at 07:20
  • no, you can get all the data you need from [ScaleUpdateDetails](https://api.flutter.dev/flutter/gestures/ScaleUpdateDetails-class.html) - they say it clearly in the error message: "scale is a superset of pan" – pskink Apr 17 '23 at 07:40
  • Yes sir! Then how do u get the data? What is the workaround? That is basically my question. Apologies if my question isn't clear enough. – Pannam Apr 17 '23 at 07:49
  • check all the properties of `ScaleUpdateDetails` (the link above) - it's jut few of them (if the docs are not clear enough just `print` them in your callback) – pskink Apr 17 '23 at 07:53

1 Answers1

1

As the message says, you need to use the onScaleUpdate to handle the panning as well. I believe details.focalPointDelta can be used in the onScaleUpdate instead of the details.delta in onPanUpdate, so it will be:

    onScaleUpdate: (details) {
        double scaleFactor = details.scale;
        if (scaleFactor > 1) {
            topL[chosenPlate] !.latitude += tolerance;
            botL[chosenPlate] !.latitude -= tolerance;
            botR[chosenPlate] !.latitude -= tolerance;
            topL[chosenPlate] !.longitude -= tolerance;
            botL[chosenPlate] !.longitude -= tolerance;
            botR[chosenPlate] !.longitude += tolerance;
        } else if (scaleFactor < 1) {
            topL[chosenPlate] !.latitude -= tolerance;
            topL[chosenPlate] !.longitude -= tolerance;
            botL[chosenPlate] !.latitude += tolerance;
            botL[chosenPlate] !.longitude -= tolerance;
            botR[chosenPlate] !.latitude += tolerance;
            botR[chosenPlate] !.longitude += tolerance;
        }

        if (details.focalPointDelta.dx < 0) {
            setState(() {
                topL[chosenPlate] !.longitude -= tolerance;
                botL[chosenPlate] !.longitude -= tolerance;
                botR[chosenPlate] !.longitude -= tolerance;
            });
        } else if (details.focalPointDelta.dx > 0) {
            setState(() {
                topL[chosenPlate] !.longitude += tolerance;
                botL[chosenPlate] !.longitude += tolerance;
                botR[chosenPlate] !.longitude += tolerance;
            });
        } else if (details.focalPointDelta.dy < 0) {
            setState(() {
                topL[chosenPlate] !.latitude += tolerance;
                botL[chosenPlate] !.latitude += tolerance;
                botR[chosenPlate] !.latitude += tolerance;
            });
        } else if (details.focalPointDelta.dy > 0) {
            setState(() {
                topL[chosenPlate] !.latitude -= tolerance;
                botL[chosenPlate] !.latitude -= tolerance;
                botR[chosenPlate] !.latitude -= tolerance;
            });
        }
    },
Ivo
  • 18,659
  • 2
  • 23
  • 35