flutter_motion_sensors 0.0.1
flutter_motion_sensors: ^0.0.1 copied to clipboard
Device motion and orientation-based animations for Flutter. Supports iOS, Android, Web, Windows, macOS, and Linux with WASM compatibility.
flutter_motion_sensors #
Device motion and orientation-based animations for Flutter. Supports iOS, Android, Web, Windows, macOS, and Linux with WASM compatibility.
Features #
- π Cross-platform support: iOS, Android, Web, Windows, macOS, Linux
- π WASM compatible: Optimized for web performance
- π± Motion sensors: Accelerometer, Gyroscope, Magnetometer
- π¨ Easy integration: Simple widgets and controllers
- β‘ Real-time data: Stream-based sensor data
- π‘οΈ Fallback support: Graceful degradation when sensors unavailable
Getting Started #
Installation #
Add this to your package's pubspec.yaml
file:
dependencies:
flutter_motion_sensors: ^0.0.1
Basic Usage #
Simple Sensor Data Access
import 'package:flutter_motion_sensors/flutter_motion_sensors.dart';
// Check if sensors are available
bool available = await FlutterMotionSensors.isMotionSensorAvailable();
// Get current sensor data
AccelerometerData? accel = await FlutterMotionSensors.getAccelerometerData();
GyroscopeData? gyro = await FlutterMotionSensors.getGyroscopeData();
MagnetometerData? mag = await FlutterMotionSensors.getMagnetometerData();
// Get all sensor data at once
MotionSensorData allData = await FlutterMotionSensors.getAllMotionSensorData();
Stream-based Sensor Listening
// Listen to accelerometer events
FlutterMotionSensors.accelerometerEvents.listen((data) {
print('Accelerometer: x=${data.x}, y=${data.y}, z=${data.z}');
});
// Listen to gyroscope events
FlutterMotionSensors.gyroscopeEvents.listen((data) {
print('Gyroscope: x=${data.x}, y=${data.y}, z=${data.z}');
});
// Listen to all motion sensor events
FlutterMotionSensors.motionSensorEvents.listen((data) {
print('Motion: ${data.timestamp}');
});
Using the Controller
class _MyWidgetState extends State<MyWidget> {
late MotionSensorController _controller;
@override
void initState() {
super.initState();
_controller = MotionSensorController();
_controller.startListening();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: _controller,
builder: (context, child) {
final accel = _controller.lastAccelerometerData;
return Text('Acceleration: ${accel?.x ?? 0.0}');
},
);
}
}
Using Builder Widgets
// Accelerometer-based animation
AccelerometerBuilder(
builder: (context, data) {
if (data == null) return CircularProgressIndicator();
return Transform.rotate(
angle: data.x * 0.1,
child: Container(
width: 100,
height: 100,
color: Colors.blue,
),
);
},
)
// Gyroscope-based animation
GyroscopeBuilder(
builder: (context, data) {
if (data == null) return CircularProgressIndicator();
return Transform.translate(
offset: Offset(data.x * 10, data.y * 10),
child: Container(
width: 50,
height: 50,
color: Colors.red,
),
);
},
)
// Combined motion sensor animation
MotionBuilder(
builder: (context, data) {
if (data == null) return CircularProgressIndicator();
return Transform(
transform: Matrix4.identity()
..rotateX(data.gyroscope?.x ?? 0.0)
..rotateY(data.gyroscope?.y ?? 0.0)
..rotateZ(data.gyroscope?.z ?? 0.0),
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.purple, Colors.orange],
),
),
),
);
},
)
Advanced Usage #
Custom Sensor Types #
MotionAnimationBuilder(
sensorTypes: [SensorType.accelerometer, SensorType.gyroscope],
builder: (context, controller) {
return Column(
children: [
Text('Accel: ${controller.lastAccelerometerData?.x ?? 0.0}'),
Text('Gyro: ${controller.lastGyroscopeData?.x ?? 0.0}'),
],
);
},
)
Manual Control #
MotionAnimationBuilder(
autoStart: false,
builder: (context, controller) {
return Column(
children: [
ElevatedButton(
onPressed: () => controller.startListening(),
child: Text('Start Sensors'),
),
ElevatedButton(
onPressed: () => controller.stopListening(),
child: Text('Stop Sensors'),
),
Text('Listening: ${controller.isListening}'),
],
);
},
)
Platform Support #
Platform | Status | Notes |
---|---|---|
iOS | β Supported | Uses CoreMotion framework |
Android | β Supported | Uses Android sensors |
Web | β Supported | WASM compatible, uses DeviceMotion API |
Windows | β Supported | Uses Windows sensors |
macOS | β Supported | Uses CoreMotion framework |
Linux | β Supported | Uses Linux sensors |
Data Models #
AccelerometerData #
x
,y
,z
: Acceleration forces in m/sΒ²timestamp
: When the data was captured
GyroscopeData #
x
,y
,z
: Angular velocities in rad/stimestamp
: When the data was captured
MagnetometerData #
x
,y
,z
: Magnetic field strength in ΞΌTtimestamp
: When the data was captured
MotionSensorData #
accelerometer
: Optional accelerometer datagyroscope
: Optional gyroscope datamagnetometer
: Optional magnetometer datatimestamp
: When the data was captured
Permissions #
Android #
Add to android/app/src/main/AndroidManifest.xml
:
<uses-permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS" />
iOS #
Add to ios/Runner/Info.plist
:
<key>NSMotionUsageDescription</key>
<string>This app uses motion sensors for interactive animations.</string>
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments #
- Built with Flutter
- Uses sensors_plus for sensor data
- Follows Flutter plugin best practices