Device Orientation
A Flutter package for calculating device orientation using the device's accelerometer and compass sensors.
device_orientation provides a simple API for reading the device's current orientation and listening to orientation changes through a stream.
Features
- π± Read the current device orientation.
- π§ Use compass heading information.
- π Use accelerometer data to calculate device orientation.
- π Listen to continuous orientation updates through a stream.
- π§© Simple API with minimal setup.
- β‘ Built specifically for Flutter.
Getting started
Add device_orientation to your pubspec.yaml:
dependencies:
device_orientation: ^0.1.0
Then import the package:
import 'package:device_orientation/device_orientation.dart';
Make sure the required platform permissions and sensor capabilities are available on the target device.
Usage
Create a DeviceOrientation instance and start listening for orientation updates:
final orientation = DeviceOrientation();
orientation.start();
orientation.stream.listen((data) {
print(data);
});
You can also access the most recently calculated orientation synchronously:
final currentOrientation = orientation.data;
When the orientation service is no longer needed, stop it and release its resources:
orientation.stop();
orientation.dispose();
Complete example
import 'package:device_orientation/device_orientation.dart';
void main() {
final orientation = DeviceOrientation();
orientation.start();
orientation.stream.listen((data) {
print(data);
});
// Access the latest value at any time.
print(orientation.data);
}
SensorService
Responsible for collecting raw sensor data from:
- Accelerometer
- Compass
It does not perform orientation calculations.
OrientationEngine
Receives the raw sensor data and calculates the resulting orientation.
DeviceOrientation
Provides the public API of the package.
It connects the sensor service and orientation engine and exposes the calculated result through:
dataβ the latest orientation.streamβ continuous orientation updates.
Platform support
The package depends on device sensors, so behavior depends on the sensors available on the target device.
| Platform | Support |
|---|---|
| Android | β |
| iOS | β |
| Web | β οΈ Sensor support may vary |
| Windows | β οΈ Sensor support may vary |
| macOS | β οΈ Sensor support may vary |
| Linux | β οΈ Sensor support may vary |
For mobile applications, testing on a physical device is recommended because sensor availability and behavior can differ between devices.
Example
A complete example application is available in the example/ directory.
The example demonstrates how to:
- Create a
DeviceOrientationinstance. - Start sensor collection.
- Listen for orientation updates.
- Display the calculated orientation.
- Stop and dispose of the service.
License
This package is licensed under the MIT License.
See the LICENSE file for details.