androidflutterdartvibration

Vibration non stop using flutter


I want to write a function when tapped I want to make phone vibrate non stop .

onPressed: () 
{
  Vibration.vibrate(duration: 100000,  );
},

how can I do it?


Solution

  • You could do something like this

    import 'dart:async';
    
    import 'package:flutter/material.dart';
    import 'package:vibration/vibration.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        bool _cancel = false;
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: Scaffold(
            appBar: AppBar(
              title: const Text("Vibration Demo"),
            ),
            body: Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  RaisedButton(
                    onPressed: () {
                      Timer.periodic(
                        const Duration(seconds: 1),
                        (Timer timer) {
                          if (_cancel) {
                            timer.cancel();
                            _cancel = false;
                            return false;
                          }
                          return Vibration.vibrate(duration: 1000);
                        },
                      );
                    },
                    child: const Text("Start Vibrations"),
                  ),
                  RaisedButton(
                    onPressed: () {
                      _cancel = true;
                    },
                    child: const Text("Stop Vibrations"),
                  )
                ],
              ),
            ),
          ),
        );
      }
    }