27

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

Miha
  • 13,223
  • 2
  • 15
  • 16

5 Answers5

36

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

Miha
  • 13,223
  • 2
  • 15
  • 16
  • 2
    I've created a quick tutorial for implementing vibration with Flutter, in case you struggle with it. Have a look: https://medium.com/tagmalogic/implement-vibration-with-flutter-c0a7d2d4007?source=friends_link&sk=6976429f156a8a7095e939f85be62f3e – Miha Jan 19 '20 at 15:41
  • Thanks, do you know why it wouldn't work for a Samsung S8? None of the methods work. It's funny because using a plugin like Vibrate at least a few of the vibration methods work. This is on Android 9 btw, and we have the manifest entry. – DarkNeuron Jan 29 '20 at 18:44
  • @DarkNeuron maybe worth having a look in `Settings` then `Sounds and vibration` and then `Vibration feedback` and turn that on if not done already. – Miha Jan 29 '20 at 21:13
  • 1
    @Miha That was turned on, but we found another setting called "Touch vibration". So there's effectively 2 settings for vibrations. With Touch vibration turned on, some of the vibrate functions worked. I think it was "heavy" and "selection" that didn't. – DarkNeuron Jan 30 '20 at 14:26
  • 1
    I dont think HapticFeedback support Flutter web correct? – Yuhao Sep 24 '20 at 15:43
19

The simplest solution for Flutter 2021

1) Import standard services:

import 'package:flutter/services.dart';

2) Use:

HapticFeedback.mediumImpact();

or lightImpact, or heavyImpact

3) Don't forget to add this at Android Manifest:

<uses-permission android:name="android.permission.VIBRATE"/>

BONUS) Play "Click" Sound:

SystemSound.play(SystemSoundType.click);
Ilya Iksent
  • 1,425
  • 17
  • 15
5

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

laaasBIGL
  • 811
  • 1
  • 7
  • 17
  • Can you please share a .dart file with a working example. Cannot figure out how to implement the code above. I've added dependencies in the .yaml file above. I was even using the example provided in the plugin here `https://github.com/clovisnicolas/flutter_vibrate/blob/master/example/lib/main.dart` but when running on iOS (iPhone 7 iOS 12.1.4) I get the following error: `Error output from CocoaPods: ↳ .... Finished with error: Error running pod install` – Miha Jun 02 '19 at 11:17
  • full error message: `Error output from CocoaPods: ↳ [!] `` attempted to initialize an object with an unknown UUID. `xxx` for attribute: `children`. This can be the result of a merge and the unknown UUID is being discarded. [!] Automatically assigning platform `ios` with version `8.0` on target `Runner` because no platform was specified. Please specify a platform for this target in your Podfile. See `https://guides.cocoapods.org/syntax/podfile.html#platform`. Finished with error: Error running pod install` – Miha Jun 02 '19 at 11:22
  • Just a follow up on this @BastianMeyer just in case you know a way to implement HapticFeedback in Flutter for Android (not using the plugin above). I can get it working for iOS. – Miha Jun 05 '19 at 12:54
4

I will recommend to use flutter_vibrate, it will work for both ios/ android. Simplier setup and works very well.

  1. Just add flutter_vibrate as a dependency in your pubspec.yaml file.

    dependencies: vibration: ^1.3.0

  2. Make sure you add the following permissions to your Android Manifest

  3. Import the package:

    import 'package:vibration/vibration.dart';

Examples:

Vibrate for default 500ms:

Vibration.vibrate();
       

Vibrate for 1000ms:

Vibration.vibrate(duration: 1000);
           

Pattern: wait 0.5s, vibrate 1s, wait 0.5s, vibrate 2s, wait 0.5s, vibrate 3s, wait 0.5s, vibrate 0.5s:

Vibration.vibrate(pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500]);
            

Pattern: wait 0.5s, vibrate 1s, wait 0.5s, vibrate 2s, wait 0.5s, vibrate 3s, wait 0.5s, vibrate 0.5s':

Vibration.vibrate( pattern: [500, 1000, 500, 2000, 500, 3000, 500, 500], intensities: [128, 255, 64, 255]);

Android

The VIBRATE permission is required in AndroidManifest.xml.

Supports vibration with duration and pattern. On Android 8 (Oreo) and above, uses the [VibrationEffect][1] class. For the rest of the usage instructions, see [Vibrator][1] class documentation.

iOS

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.

Rohan Arora
  • 303
  • 2
  • 12
Cassio Seffrin
  • 7,293
  • 1
  • 54
  • 54
0
import 'package:flutter/material.dart';
import 'package:vibrate/vibrate.dart';

// Note:
// Make sure you add the following permissions to your Android Manifest
// <uses-permission android:name="android.permission.VIBRATE"/>
// 
// In pubspec.yaml file, add following dependency
// dependencies:
//   vibrate: ^0.0.4

class TestVibration extends StatefulWidget {
  @override
  _TestVibrationState createState() => _TestVibrationState();
}

class _TestVibrationState extends State<TestVibration> {

  bool canVibrate = false;
  @override
  void initState() {
    super.initState();
    _checkIfVibrate();
  }
  _checkIfVibrate() async {
    // check if device can vibrate
    canVibrate = await Vibrate.canVibrate;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: RaisedButton(
        child: Text('Vibrate'),
        onPressed: (){
          // FeedbackTypes -> {success, error, warning, selection, impact, heavy, medium, light}
          _getVibration(FeedbackType.warning);
        },
      ),
    );
  }

  _getVibration(feedbackType) async {
    if (canVibrate) {
      Vibrate.feedback(feedbackType);
      // Vibrate.vibrate();   // Try this too!
    }
  }
}
shrSachin
  • 1
  • 1