skip to Main Content

a button should be long pressed to record something. If during the long press a swipe is detected the record should be deleted.

I tried to combine onLongPressStart/End with onPanUpdate, but it doesn’t work

     GestureDetector(
      child: Icon(Icons.mic_none_outlined),
      onLongPressStart: (_) async {
        setState(() {
          startRecording = true;  //start recording
        });
        do {
          await Future.delayed(Duration(seconds: 1));
        } while (startRecording );
      },
      onLongPressEnd:  (_) async {
        setState(() {
          startRecording = false; //end recording
        });
      },
      onPanUpdate: (details) {              //<----- not working
        // Swiping in left direction    
        if (details.delta.dx < 0) {
          //delete record
        }
      },
    ),

2

Answers


  1. Chosen as BEST ANSWER

    this code works:

     GestureDetector(
      child: Icon(Icons.mic_none_outlined),
      onLongPressStart: (_) async {
        setState(() {
          startRecording = true;  //start recording
        });
        do {
          await Future.delayed(Duration(seconds: 1));
        } while (startRecording );
      },
      onLongPressEnd:  (_) async {
        setState(() {
          startRecording = false; //end recording
        });
      },
      onLongPressMoveUpdate:(movement) async {
        if (movement.offsetFromOrigin.dx < -300) {
          print("------------------swipe detection------------------------");
        }
          },
    ),
    

  2. GestureDetector(
        onHorizontalDragUpdate: (details) {  
            // Note: Sensitivity is integer used when you don't want to mess up vertical drag
            int sensitivity = 8;
            if (details.delta.dx > sensitivity) {
                // Right Swipe
            } else if(details.delta.dx < -sensitivity){
                //Left Swipe
            }
        }
    );
    

    Use OnHorizontalDragUpdate

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search