shake 2.1.0 shake: ^2.1.0 copied to clipboard
A flutter package to detect phone shakes. Adjustable G-force and reset periods.
import 'package:flutter/material.dart';
import 'package:shake/shake.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: DemoPage(),
);
}
}
class DemoPage extends StatefulWidget {
@override
_DemoPageState createState() => _DemoPageState();
}
class _DemoPageState extends State<DemoPage> {
@override
void initState() {
super.initState();
ShakeDetector detector = ShakeDetector.autoStart(
onPhoneShake: () {
ScaffoldMessenger.of(context)
.showSnackBar(SnackBar(content: Text('Shake!')));
// Do stuff on phone shake
},
minimumShakeCount: 1,
shakeSlopTimeMS: 500,
shakeCountResetTime: 3000,
shakeThresholdGravity: 2.7,
);
// To close: detector.stopListening();
// ShakeDetector.waitForStart() waits for user to call detector.startListening();
}
@override
Widget build(BuildContext context) {
return Scaffold();
}
}