https://github.com/tpucci/shake_gesture
0 dependencies Flutter plugin that detects shake gestures on Android and iOS (even on simulators).
https://github.com/tpucci/shake_gesture
flutter flutter-plugin
Last synced: 4 months ago
JSON representation
0 dependencies Flutter plugin that detects shake gestures on Android and iOS (even on simulators).
- Host: GitHub
- URL: https://github.com/tpucci/shake_gesture
- Owner: tpucci
- License: mit
- Created: 2023-08-03T19:22:14.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-10-07T08:32:44.000Z (9 months ago)
- Last Synced: 2026-02-22T23:01:06.837Z (4 months ago)
- Topics: flutter, flutter-plugin
- Language: Kotlin
- Homepage:
- Size: 174 KB
- Stars: 12
- Watchers: 0
- Forks: 4
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# shake_gesture
This Flutter plugin detects shake gestures on Android and iOS.
> This plugin has 0 dependencies 🚀
> This plugin works on simulators 🤖
## Usage
### Imperatively
```dart
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
```dart
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:
```kotlin
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`:
```dart
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`](https://developer.apple.com/documentation/uikit/uievent/eventsubtype/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:
```xml
```
## Contribute
Test your contribution by running the unit tests and integration tests.
```sh
cd shake_gesture/example
flutter test
flutter test integration_test
```