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:

  1. Create a DeviceOrientation instance.
  2. Start sensor collection.
  3. Listen for orientation updates.
  4. Display the calculated orientation.
  5. Stop and dispose of the service.

License

This package is licensed under the MIT License.

See the LICENSE file for details.