Flutter Shake Detector doesn't detect phone's shake

822 Views Asked by At

I'm using Shake Flutter package in order to detect shakes on my application, but it doesn't seem to work either on my physical device (Samsung Galaxy A71) or on my Android Emulator.

Here is how I initialise the ShakeDetector:

class _HomePageState extends State<HomePage> {
  bool _hasShaked;
  ShakeDetector _shakeDetector;

  @override
  void initState() {
    super.initState();
    _hasShaked = false;
    _shakeDetector = ShakeDetector.autoStart(
      onPhoneShake: () {
        print('shake')
        setState(() {
          _hasShaked = true;
          _shakeDetector.stopListening();
        });
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    // ...
  }
}

I tried to use ShakeDetector#waitForStart() instead but it doesn't work either:

class _HomePageState extends State<HomePage> {
  bool _hasShaked;
  ShakeDetector _shakeDetector;

  @override
  void initState() {
    super.initState();
    _hasShaked = false;
    _shakeDetector = ShakeDetector.waitForStart(
      onPhoneShake: () {
        print('shake')
        setState(() {
          _hasShaked = true;
          _shakeDetector.stopListening();
        });
      },
    );
  _shakeDetector.startListening();
  }

  @override
  Widget build(BuildContext context) {
    // ...
  }
}

Here is my dependencies in my pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.3
  shake: ^0.1.0

Thanks for the help.

0

There are 0 best solutions below