Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 776
Shashwat Vinod Singhal Avatar asked Mar 05 '23 18:03

Shashwat Vinod Singhal


1 Answers

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.

like image 123
Niraj Niroula Avatar answered Mar 19 '23 09:03

Niraj Niroula