123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import 'package:flutter/material.dart';
- import 'package:vibrate/vibrate.dart';
-
- void main() => runApp(new MyApp());
-
- class MyApp extends StatefulWidget {
- @override
- _MyAppState createState() => new _MyAppState();
- }
-
- class _MyAppState extends State<MyApp> {
- bool _canVibrate = true;
- final Iterable<Duration> pauses = [
- const Duration(milliseconds: 500),
- const Duration(milliseconds: 1000),
- const Duration(milliseconds: 500),
- ];
-
- @override
- initState() {
- super.initState();
- init();
- }
-
- init() async {
- bool canVibrate = await Vibrate.canVibrate;
- setState(() {
- _canVibrate = canVibrate;
- _canVibrate
- ? print("This device can vibrate")
- : print("This device cannot vibrate");
- });
- }
-
- @override
- Widget build(BuildContext context) {
- return new MaterialApp(
- home: new Scaffold(
- appBar: new AppBar(title: new Text('Haptic Feedback Example')),
- body: new Center(
- child: new Column(children: <Widget>[
- new ListTile(
- title: new Text("Vibrate"),
- leading: new Icon(Icons.vibration, color: Colors.teal),
- onTap: !_canVibrate
- ? null
- : () {
- Vibrate.vibrate();
- },
- ),
- new ListTile(
- title: new Text("Vibrate with Pauses"),
- leading: new Icon(Icons.vibration, color: Colors.brown),
- onTap: !_canVibrate
- ? null
- : () {
- Vibrate.vibrateWithPauses(pauses);
- },
- ),
- new Divider(height: 1.0),
- new ListTile(
- title: new Text("Impact"),
- leading: new Icon(Icons.tap_and_play, color: Colors.orange),
- onTap: !_canVibrate
- ? null
- : () {
- Vibrate.feedback(FeedbackType.impact);
- },
- ),
- new ListTile(
- title: new Text("Selection"),
- leading: new Icon(Icons.select_all, color: Colors.blue),
- onTap: !_canVibrate
- ? null
- : () {
- Vibrate.feedback(FeedbackType.selection);
- },
- ),
- new ListTile(
- title: new Text("Success"),
- leading: new Icon(Icons.check, color: Colors.green),
- onTap: !_canVibrate
- ? null
- : () {
- Vibrate.feedback(FeedbackType.success);
- },
- ),
- new ListTile(
- title: new Text("Warning"),
- leading: new Icon(Icons.warning, color: Colors.red),
- onTap: !_canVibrate
- ? null
- : () {
- Vibrate.feedback(FeedbackType.warning);
- },
- ),
- new ListTile(
- title: new Text("Error"),
- leading: new Icon(Icons.error, color: Colors.red),
- onTap: !_canVibrate
- ? null
- : () {
- Vibrate.feedback(FeedbackType.error);
- },
- ),
- new Divider(height: 1.0),
- new ListTile(
- title: new Text("Heavy"),
- leading:
- new Icon(Icons.notification_important, color: Colors.red),
- onTap: !_canVibrate
- ? null
- : () {
- Vibrate.feedback(FeedbackType.heavy);
- },
- ),
- new ListTile(
- title: new Text("Medium"),
- leading:
- new Icon(Icons.notification_important, color: Colors.green),
- onTap: !_canVibrate
- ? null
- : () {
- Vibrate.feedback(FeedbackType.medium);
- },
- ),
- new ListTile(
- title: new Text("Light"),
- leading: new Icon(Icons.notification_important,
- color: Colors.yellow[700]),
- onTap: !_canVibrate
- ? null
- : () {
- Vibrate.feedback(FeedbackType.light);
- },
- ),
- ]),
- ),
- ),
- );
- }
- }
|