— Flutter, Dart, Flutter Tips — 1 min read
There are a couple of different packages that you can use to implement vibrations in Flutter, but there's also actually a way to implement it without adding any new dependencies at all by using the HapticFeedback class. It's probably worth mentioning that this may not work with older devices because it utilizes Haptic Feedback...
First of all you'll need to add the vibrations permission below to your AndroidManifest.xml file(for Android support):
1<uses-permission android:name="android.permission.VIBRATE" />
To use HapticFeedback
you need to import services.dart
by adding the following line to your file:
1import 'package:flutter/services.dart';
After you add the above import you can use the HapticFeedback
class. Probably the most obvious method in the class is the vibrate
method:
1HapticFeedback.vibrate();
There's also a few other methods that you can use, which cause a vibration(or a 'feedback sensation') at various different levels:
1HapticFeedback.lightImpact();2HapticFeedback.mediumImpact();3HapticFeedback.heavyImpact();
And another method that simulates selection feedback:
1HapticFeedback.selectionClick();
And if you're looking for something a bit more advanced you could also checkout the vibration
package on pub.dev.