Skip to main content

Flutter Vibrate Examples

Flutter Vibrate: How To Examples

Introduction

Flutter Vibrate is a Flutter plugin that allows you to vibrate the device. It provides an easy way to add haptic feedback to your Flutter applications. In this tutorial, we will cover the installation process and provide step-by-step examples of how to use the Flutter Vibrate plugin.

Installation

To use the Flutter Vibrate plugin in your Flutter project, follow these steps:

  1. Open your project's pubspec.yaml file.
  2. Add the following dependency under the dependencies section:
    dependencies:
    flutter_vibrate: ^1.0.2
  3. Save the file and run the command flutter pub get in your terminal or IDE to fetch the package.

Example 1: Vibrate the Device

To vibrate the device, use the Vibrate.vibrate() method. Here's an example:

import 'package:flutter/material.dart';
import 'package:flutter_vibrate/flutter_vibrate.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {

Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Vibrate Example'),
),
body: Center(
child: RaisedButton(
child: Text('Vibrate'),
onPressed: () {
Vibrate.vibrate();
},
),
),
),
);
}
}

When you run the above code, a button labeled "Vibrate" will appear on the screen. When you tap the button, the device will vibrate.

Example 2: Vibrate with Duration

You can specify the duration of the vibration using the Vibrate.vibrate(duration: Duration) method. Here's an example:

// ...
onPressed: () {
Vibrate.vibrate(duration: Duration(milliseconds: 500));
},
// ...

In this example, the device will vibrate for 500 milliseconds when the button is pressed.

Example 3: Vibrate with Pattern

You can create custom vibration patterns using the Vibrate.vibrate(pattern: List<int>) method. The pattern is defined as a list of durations in milliseconds. Here's an example:

// ...
onPressed: () {
Vibrate.vibrate(pattern: [100, 200, 300, 400, 500]);
},
// ...

In this example, the device will vibrate with the pattern [100, 200, 300, 400, 500] when the button is pressed.

Example 4: Check Vibration Support

You can check if the device supports vibration using the Vibrate.hasVibrator() method. Here's an example:

// ...
onPressed: () {
bool hasVibrator = Vibrate.hasVibrator();
print('Device has vibrator: $hasVibrator');
},
// ...

When you run the above code, it will print whether the device has a vibrator or not.

Example 5: Check iOS Haptic Feedback Support

You can check if the device supports iOS haptic feedback using the Vibrate.hasCustomVibrationsSupport() method. Here's an example:

// ...
onPressed: () {
bool hasCustomVibrationsSupport = Vibrate.hasCustomVibrationsSupport();
print('Device has custom vibrations support: $hasCustomVibrationsSupport');
},
// ...

When you run the above code on an iOS device, it will print whether the device supports custom vibrations or not.

Example 6: Vibrate with iOS Haptic Feedback

On iOS, you can use custom haptic feedback patterns. Here's an example:

// ...
onPressed: () {
Vibrate.feedback(FeedbackType.success);
},
// ...

In this example, the device will generate a success haptic feedback pattern when the button is pressed. You can use other FeedbackType values like warning, error, light, and medium.

Example 7: Cancel Vibration

You can cancel the ongoing vibration using the Vibrate.cancel() method. Here's an example:

// ...
onPressed: () {
Vibrate.vibrate();
Future.delayed(Duration(seconds: 1), () {
Vibrate.cancel();
});
},
// ...

In this example, the device will vibrate for 1 second and then the vibration will be canceled.

Example 8: Check iOS Haptic Feedback Availability

You can check if the device supports iOS haptic feedback using the Vibrate.isCustomVibrationsAvailable() method. Here's an example:

// ...
onPressed: () async {
bool isCustomVibrationsAvailable = await Vibrate.isCustomVibrationsAvailable();
print('Custom vibrations available: $isCustomVibrationsAvailable');
},
// ...

When you run the above code on an iOS device, it will print whether custom vibrations are available or not.

Example 9: Vibrate with Repeat

You can make the vibration repeat using the Vibrate.vibrateWithPauses() method. Here's an example:

// ...
onPressed: () {
Vibrate.vibrateWithPauses([100, 200, 300], repeat: true);
},
// ...

In this example, the device will vibrate with the pattern [100, 200, 300] repeatedly when the button is pressed.

Example 10: Vibrate with Amplitude

You can control the vibration amplitude on Android using the Vibrate.vibrateWithAmplitude() method. Here's an example:

// ...
onPressed: () {
Vibrate.vibrateWithAmplitude(100);
},
// ...

In this example, the device will vibrate with an amplitude of 100 when the button is pressed.

Conclusion

In this tutorial, we have learned how to install the Flutter Vibrate plugin and use it to add haptic feedback to your Flutter applications. We covered various examples, including basic vibration, vibration with duration, vibration with pattern, checking device support, iOS haptic feedback, canceling vibration, and more. Use these examples as a starting point to enhance the user experience in your Flutter projects.