I've installed your package, but it's not detecting shakes for some reason.
I've tried both methods to initialise it, but neither seems to work.
I'm using a real device. A pixel 3.
main.dart contains the below code.
No errors are output, but shakes are not detected for some reason. Please can you help?
Many thanks.
import 'package:flutter/material.dart';
import 'package:shake/shake.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Shake Test',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Shake Test'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
// ShakeDetector detector = ShakeDetector.waitForStart(onPhoneShake: () {
// // Do stuff on phone shake
// print('shake');
// _incrementCounter();
// });
// detector.startListening();
ShakeDetector detector = ShakeDetector.autoStart(onPhoneShake: () {
// Do stuff on phone shake
print('shake');
_incrementCounter();
});
}
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}