Download StackFood Multi Restaurant, Food Delivery App with Laravel Admin and Restaurant Panel Buy Full App on $79
Download FlutKit - Flutter UI Kit Most beautiful Flutter UI Kit with 20+ Full App, 450+ Screens. Buy Full Kit on $39
Download Fluxstore Multi Vendor WooCommerce/Dokan Plugin compatible WordPress eCommerce Flutter App. Buy Full App on $79

How to Programmatically Vibrate Device with Dart in Flutter App

In this example, we are going to show you the easiest way to implement vibration programmatically with Dart in Flutter App. You can control duration and pattern of vibration.

First, you need to add vibration Flutter package in your project depedency by adding following lines in pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  vibration: ^1.7.3

Import Package to Script:

import 'package:vibration/vibration.dart';

Add Vibration permission in android/app/src/main/AndroidManifest.xml

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

How to Vibrate Device in Flutter App:

if (await Vibration.hasVibrator()) { //check if device has vibration feature
    Vibration.vibrate(); //500 millisecond vibration
}

How to Vibrate Device with Duration Control:

Vibration.vibrate(duration: 1000); //1 Sec = 1000 millisecond

How to Vibrate Device on Wait and Vibrate duration pattern:

Vibration.vibrate(
  pattern: [
      500, //wait 0.5 second
      1000, //vibrate 1 second
      500, //wait 0.5 second
      3000 //vibrate 3 second.
      //add mroe pattern here
    ]
); //1 Sec = 1000 millisecond

Full Code Example:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
     return Scaffold(
        appBar: AppBar(
           title: Text("Device Vibration with Flutter"),
           backgroundColor: Colors.redAccent,
        ),
        body: Container(
          padding: EdgeInsets.all(20),
           child: Column(
             crossAxisAlignment: CrossAxisAlignment.center,
             children: [

               Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.all(5),
                  child: ElevatedButton(
                     child: Text("Vibrate Default"),
                     onPressed: () async {
                       if (await Vibration.hasVibrator()) { //check if device has vibration feature
                             Vibration.vibrate(); //500 millisecond vibration
                       }
                     },
                  ),
                ),
                
                Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.all(5),
                  child: ElevatedButton(
                     child: Text("Vibrate For 1 Sec"),
                     onPressed: (){
                       Vibration.vibrate(duration: 1000); //1 Sec = 1000 millisecond
                     },
                  ),
                ),

                Container(
                  alignment: Alignment.center,
                  padding: EdgeInsets.all(5),
                  child: ElevatedButton(
                     child: Text("Vibrate With Pattern"),
                     onPressed: (){
                       Vibration.vibrate(
                         pattern: [
                              500, //wait 0.5 second
                              1000, //vibrate 1 second
                              500, //wait 0.5 second
                              3000 //vibrate 3 second.
                              //add mroe pattern here
                            ]
                       ); //1 Sec = 1000 millisecond
                     },
                  ),
                )

           ],)
        ),
     );
  }

}

Output Screenshot:

In this way, you can vibrate device programmatically with Dart in Flutter App.

No any Comments on this Article


Please Wait...