shake_gesture 1.1.0 copy "shake_gesture: ^1.1.0" to clipboard
shake_gesture: ^1.1.0 copied to clipboard

Provides a widget to detect shake gestures. It allows you to easily add shake detection to your Flutter app on Android and iOS.

shake_gesture #

This Flutter plugin detects shake gestures on Android and iOS.

This plugin has 0 dependencies 🚀

This plugin works on simulators 🤖

Usage #

Imperatively #

void main() {
  void myCallback() {}

  // Register the callback
  ShakeGesture.registerCallback(onShake: myCallback)

  // In dispose functions, don't forget to clean up
  ShakeGesture.unregisterCallback(onShake: myCallback)
}

Widget #

class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('ShakeGesture Example')),
      body: Center(

		// Here it is 👇

        child: ShakeGesture(
          onShake: () {
            ScaffoldMessenger.of(context).showSnackBar(
              const SnackBar(content: Text('Shake!')),
            );
          },
          child: const Center(
            child: OutlinedButton(
              onPressed: ShakeGestureTestHelperExtension.simulateShake,
              child: Text('Simulate Shake'),
            ),
          ),
        ),

		// The end.

      ),
    );
  }
}

Simulator #

This package works in the iOS simulator.

To simulate a shake event in Android emulator, either play with the Sensor Manager, or add the following piece of code to your Activity:

import android.view.KeyEvent
import dev.fluttercommunity.shake_gesture_android.ShakeGesturePlugin

class MainActivity: FlutterActivity() {

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            this.flutterEngine?.plugins?.get(ShakeGesturePlugin::class.java).let { plugin ->
                if (plugin is ShakeGesturePlugin)
                    plugin.onShake()
            }
        }

        return super.onKeyDown(keyCode, event)
    }

}

Then, you can use ctrl+m or cmd+m (mac) to simulate a shake motion.

Test Helper #

In order to simulate a shake gesture in a test, add the following package:

shake_gesture_test_helper

And call the shake method on your widgetTester:

    testWidgets('it detects shakes', (widgetTester) async {
      var shakeDetected = false;
      await widgetTester.pumpWidget(
        ShakeGesture(
          onShake: () {
            shakeDetected = true;
          },
          child: Container(),
        ),
      );
      await widgetTester.shake();
      expect(shakeDetected, true);
    });

Customize required shake gesture #

iOS #

Unfortunatly, you can not customize the shake gesture on iOS. Indeed, this package depends on the Apple SDK's motionShake.

Android #

By default, the required shake force is 6 Newtons and the required number of shakes is 6. This can be overriden in your AndroidManifest.xml file:

<manifest ...>
    <application ...>
        <meta-data
            android:name="dev.fluttercommunity.shake_gesture_android.SHAKE_FORCE"
            android:value="4" />
        <meta-data
            android:name="dev.fluttercommunity.shake_gesture_android.MIN_NUM_SHAKES"
            android:value="3" />
    </application>
</manifest>

Contribute #

Test your contribution by running the unit tests and integration tests.

cd shake_gesture/example
flutter test
flutter test integration_test
16
likes
150
points
16.7k
downloads

Publisher

verified publisherthomaspucci.com

Weekly Downloads

Provides a widget to detect shake gestures. It allows you to easily add shake detection to your Flutter app on Android and iOS.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, shake_gesture_android, shake_gesture_ios, shake_gesture_platform_interface

More

Packages that depend on shake_gesture