Add vibration
to the dependencies section of pubspec.yaml
.
dependencies:
vibration: ^1.7.5
Import package:
import 'package:vibration/vibration.dart';
Check if the target device has vibration capabilities.
if (await 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 (await Vibration.hasAmplitudeControl()) {
Vibration.vibrate(amplitude: 128);
}
Check if the device is able to vibrate with a custom duration, pattern or intensity. May return true
even if the device has no vibrator (if you want to check whether the device has a vibrator, see hasVibrator
).
if (await Vibration.hasCustomVibrationsSupport()) {
Vibration.vibrate(duration: 1000);
} else {
Vibration.vibrate();
await Future.delayed(Duration(milliseconds: 500));
Vibration.vibrate();
}
Vibration.vibrate(duration: 1000);
Default duration is 500ms.
Vibration.vibrate(duration: 1000, amplitude: 128);
Vibration.vibrate(pattern: [500, 1000, 500, 2000]);
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.
Supports vibration with duration and pattern on CoreHaptics devices. On older devices, the pattern is emulated with 500ms long vibrations. You can check whether the current device has CoreHaptics support using hasCustomVibrationsSupport
.
Run this command:
With Flutter:
$ flutter pub add vibration
This will add a line like this to your package's pubspec.yaml (and run an implicit flutter pub get
):
dependencies:
vibration: ^1.8.1
Alternatively, your editor might support flutter pub get
. Check the docs for your editor to learn more.
Now in your Dart code, you can use:
import 'package:vibration/vibration.dart';
import 'package:flutter/material.dart';
import 'package:vibration/vibration.dart';
void main() => runApp(VibratingApp());
class VibratingApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Vibration Plugin example app'),
),
body: Builder(
builder: (BuildContext context) {
return Center(
child: Column(
children: <Widget>[
ElevatedButton(
child: Text('Vibrate for default 500ms'),
onPressed: () {
Vibration.vibrate();
},
),
ElevatedButton(
child: Text('Vibrate for 1000ms'),
onPressed: () {
Vibration.vibrate(duration: 1000);
},
),
ElevatedButton(
child: Text('Vibrate with pattern'),
onPressed: () {
final snackBar = SnackBar(
content: Text(
'Pattern: wait 0.5s, vibrate 1s, wait 0.5s, vibrate 2s, wait 0.5s, vibrate 3s, wait 0.5s, vibrate 0.5s',
),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
Vibration.vibrate(
pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500],
);
},
),
ElevatedButton(
child: Text('Vibrate with pattern and amplitude'),
onPressed: () {
final snackBar = SnackBar(
content: Text(
'Pattern: wait 0.5s, vibrate 1s, wait 0.5s, vibrate 2s, wait 0.5s, vibrate 3s, wait 0.5s, vibrate 0.5s',
),
);
ScaffoldMessenger.of(context).showSnackBar(snackBar);
Vibration.vibrate(
pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500],
intensities: [0, 128, 0, 255, 0, 64, 0, 255],
);
},
)
],
),
);
},
),
),
);
}
}
Download details:
Author: benjamindean
Source: https://github.com/benjamindean/flutter_vibration