How to make phone vibrate with Flutter

Title is pretty straight forward.

First, import the vibrate package from pub. Also, check out how to set it up.

Create a new dart file and paste this code:

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

class VibrateExample extends StatefulWidget {
  @override
  _VibrateExampleState createState() => _VibrateExampleState();
}

class _VibrateExampleState extends State<VibrateExample> {
  bool _canVibrate = false;

  @override
  void initState() {
    super.initState();
    _checkVibration();
  }

  _checkVibration() async {
    _canVibrate = await Vibrate.canVibrate;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Vibrate Phone Example')),
      body: Center(
        child: _canVibrate
            ? RaisedButton(
                child: Text("Vibrate Phone"),
                color: Colors.red,
                onPressed: () => Vibrate.vibrate())
            : Text("Phone Cannot Vibrate"),
      ),
    );
  }
}

In main.dart:

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Code Snippets',
      theme: new ThemeData(primarySwatch: Colors.red),
      home: new VibrateExample(),
    );
  }
}

If you have any questions or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

50% Discounts On Projects

Do you have a custom project with a clear requirements, we are currently giving a 50% discount on all projects.

We will work with you to ensure we deliver what you need.

If you have questions or want to chat with us, you can reach us on Skype (henry.tochi)

Languages and technologies we use - Java, Kotlin, Laravel, Vue, React, Flutter, Swift and more.