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
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With