Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way to check Left/Right Drag in #flutter?

Is there a better way to check Left/Right Drag in #flutter. I have done it but somtime it works sometime it doesn't.

  new GestureDetector(
  onHorizontalDragEnd: (DragEndDetails details) {
      print("Drag Left - AddValue");

    setState((){
      _value++;
    });
    if (details.velocity.pixelsPerSecond.dx > -1000.0) {
      print("Drag Right - SubValue");

      setState((){
        _value--;
      });
    }
  },
  child: new Container(
    child:new Text("$_value"),
  ),
);
like image 737
Ajay Kumar Avatar asked Oct 02 '17 15:10

Ajay Kumar


1 Answers

using the GestureDetector widget and its panUpdate method, calculate the distance moved.

    GestureDetector(
     onPanStart: (DragStartDetails details) {
      initial = details.globalPosition.dx;
     },
     onPanUpdate: (DragUpdateDetails details) {
      distance= details.globalPosition.dx - initial;  
     },
     onPanEnd: (DragEndDetails details) {
      initial = 0.0; 
     print(distance);
     //+ve distance signifies a drag from left to right(start to end)
     //-ve distance signifies a drag from right to left(end to start)
});
like image 162
Gabriel Ezenwankwo Avatar answered Sep 22 '22 13:09

Gabriel Ezenwankwo