A plugin for handling Vibration API on iOS and Android devices. API docs.
-
Add
vibration
to the dependencies section ofpubspec.yaml
.dependencies: vibration: 1.2.1
-
Import package:
import 'package:vibration/vibration.dart';
Check if the target device has vibration capabilities.
if (Vibration.hasVibrator()) {
Vibration.vibrate();
}
Check if the target device has the ability to control the vibration amplitude, introduced in Android 8.0 Oreo - false for all earlier API levels.
if (Vibration.hasAmplitudeControl()) {
Vibration.vibrate(amplitude: 128);
}
Vibration.vibrate(duration: 1000);
Default duration is 500ms.
Vibration.vibrate(duration: 1000, amplitude: 128);
Vibration.vibrate(pattern: [500, 1000, 500, 2000]);
With pattern (wait 500ms, vibrate 1s, wait 500ms, vibrate 2s) at varying intensities (1 - min, 255 - max):
Vibration.vibrate(pattern: [500, 1000, 500, 2000], intensities: [1, 255]);
Stop ongoing vibration.
Vibration.cancel();
The VIBRATE
permission is required in AndroidManifest.xml.
<uses-permission android:name="android.permission.VIBRATE"/>
Supports vibration with duration and pattern. On Android 8 (Oreo) and above, uses the VibrationEffect class. For the rest of the usage instructions, see Vibrator class documentation.
Only supports singular vibrations with 500ms duration.