Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement vibration with Flutter for both Android and iOS?

Using Flutter I am trying to implement vibration on a button click.

I find it surprisingly difficult to be honest. I've tried using the following packages unsuccessfully: vibration and vibrate but they just do not vibrate (talking here about the real devices not emulators/simulators).

Is there a guide with clear examples on how to implement vibration on iOS and Android devices with Flutter?

Thanks, Mihai

like image 358
Miha Avatar asked Jun 02 '19 09:06

Miha


People also ask

How do you add haptic feedback on flutter app?

To use HapticFeedback , you'll need to import the HapticFeedback class from `flutter/services. dart'; This then gives you the ability to call the following methods: HapticFeedback.


2 Answers

Found a way using HapticFeedback.vibrate(). It works for both iOS and Android. See more details with code example and other settings on this [post][1]: Flutter: How to use HapticFeedback

like image 173
Miha Avatar answered Sep 19 '22 21:09

Miha


First, you need to add vibrate: as a dependency to your pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  vibrate:

After this, you need to import the package in the class that you are using.

// Import package
import 'package:vibrate/vibrate.dart';

Now you can this for vibration:

// Check if the device can vibrate
bool canVibrate = await Vibrate.canVibrate;

// Vibrate
// Vibration duration is a constant 500ms because
// it cannot be set to a specific duration on iOS.
Vibrate.vibrate();

// Vibrate with pauses between each vibration
final Iterable<Duration> pauses = [
    const Duration(milliseconds: 500),
    const Duration(milliseconds: 1000),
    const Duration(milliseconds: 500),
];
// vibrate - sleep 0.5s - vibrate - sleep 1s - vibrate - sleep 0.5s - vibrate
Vibrate.vibrateWithPauses(pauses);

or for haptic feedback:

// Choose from any of these available methods
enum FeedbackType {
  success,
  error,
  warning,
  selection,
  impact,
  heavy,
  medium,
  light
}

var _type = FeedbackType.impact;
Vibrate.feedback(_type);

Source: https://github.com/clovisnicolas/flutter_vibrate

like image 24
laaasBIGL Avatar answered Sep 22 '22 21:09

laaasBIGL