Search code examples
androidiosflutterhaptic-feedback

Flutter Haptic Feedback On Long Press


How are we supposed to cause haptic feedback on long press using Flutter HapticFeedback class?

I am currently working with HapticFeedback.selectionClick() during my OnTapDown method, but nothing is happening.

I have also already added the vibrate permission in the android manifest file. I am using a Pixel2 XL device for testing.


Solution

  • You can use vibrate plugin for that.
    Add this dependency in your pubspec.yaml file

    vibrate: ^0.0.4  
    

    Use this function in your class for vibration

    void vibrate() async {
        bool canVibrate = await Vibrate.canVibrate;
        canVibrate ? Vibrate.feedback(FeedbackType.medium) : null;
      }
    

    And call the above function from your widget when the onLongPress event is triggred

     onLongPress: () {
              vibrate();
            },
    

    More about vibrate plugin.
    Hope it helps.