native_device_orientation 0.3.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 96

Native Device Orientation #

pub package

This is a plugin project that allows for getting the native device orientation.

Why? #

Flutter provides a couple of way to get the 'orientation', but they all amount to basically checking whether the screen is taller or wider. This could fail for a strangely shaped device, but that isn't the primary issue.

The primary issue is that this method doesn't differentiate between landscape left and landscape right (what you get from rotation an upright phone left or right).

This isn't an issue for most applications, but when I was writing a plugin which displays a camera image, it became a problem as I need to know which way the screen is rotated.

UseSensor #

When using either the build-in widget or the plugin directly, there is an option you can pass in which is called useSensor. When it is true, the device's sensors are used directly rather than simply using the window/page orientation. By default it is false, which means the plugin doesn't to much more than simply tell you whether the window is oriented landscapeLeft or landscapeRight.

This has been tested less thoroughly than other parts of the plugin so your mileage may vary and if you run into any issues please open an issue!

Using the plugin - built-in widget #

There are two ways of using the plugin. The most basic is what is shown in the example code; this entails encapsulating your code in a NativeDeviceOrientationReader widget, and then using NativeDeviceOrientationReader.orientation(context); in a widget encapsulated within the context.

This allows you to control when the device starts listening for orientation changes (which could use a bit of energy) by deciding where the NativeDeviceOrientationReader is instantiated, while being able to access the orientation in a simple way.

Note that there could be a very slight time between when the NativeDeviceOrientationReader widget is instantiated and when the orientation is read where the widget could be built with an incorrect orientation; it uses flutter's method of size until the first message it receives back from the native code (which should be fairly immediate anyways). It assumes that landscape is right and portrait is upright during this time.

See example and source code for more details.

Using the plugin - directly #

It is also possible to bypass the helper widget to access the native calls directly. This is done by using the NativeDeviceOrientationCommunicator class. It is a singleton but can be instantiated like a normal class, and handles the communication between the ios/android code and the flutter code.

This class has two interesting methods:

  1. Future<NativeDeviceOrientation> orientation(useSensor: false): This can be called to get the orientation asynchronously.

  2. Stream<NativeDeviceOrientation> onOrientationChanged(useSensor: false): This can be called to get a stream which receives new events whenever the orientation changes. It should also get an initial value pretty much immediately.

[0.3.0] - November 6th, 2019 #

  • Breaking change to support the fact that AppLifecycleState.suspended has changed to AppLifecycleState.detached.

[0.2.0] - April 24, 2019 #

  • Slightly breaking change - on iOS, the sensor was used regardless of what was passed to useSensor due to a bug. This meant that even with UI rotation disabled, rotation events occured.
  • Also switched back to using StatusBarOrientation. It isn't actually deprecated, just not advised for normal iOS usage. However, it does what I want and is consistent with Android - it returns the orientation of the actual window. If you really want to know the orientation of the device, use useSensor
  • Add buttons to explicitly get rotation using sensor or not (UI)

[0.1.2] - March 4, 2019 #

  • Reduce amount of orientation changes by keeping track of last change.

[0.1.1] - March 4, 2019 #

  • Fix documentation and re-order changelog.

[0.1.0] - March 4, 2019 #

  • Integrate PRs updating to AndroidX and implementing useSensor
  • Also made a few changes to do with UseSensor so be careful of that if you're using the NativeDeviceOrientationCommunicator directly; users who simply use the widget shouldn't notice anything.

[0.0.4] - January 2, 2019 #

  • Switch to using UIDeviceOrientation on iOS

[0.0.2] - June 4, 2018 #

  • Fix name of package

[0.0.1] - June 4, 2018 #

  • Implement Android & iOS native code.
  • Implement Flutter code & widget.
  • Write simple example.


import 'package:flutter/material.dart';
import 'package:native_device_orientation/native_device_orientation.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  _MyAppState createState() => new _MyAppState();

class _MyAppState extends State<MyApp> {
  bool useSensor = false;

  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
          appBar: new AppBar(
            title: new Text('Native Orientation example app'),
            actions: <Widget>[Switch(value: useSensor, onChanged: (val) => setState(() => useSensor = val))],
          body: NativeDeviceOrientationReader(
            builder: (context) {
              NativeDeviceOrientation orientation = NativeDeviceOrientationReader.orientation(context);
              print("Received new orientation: $orientation");
              return Center(child: Text('Native Orientation: $orientation\n'));
            useSensor: useSensor,
          floatingActionButton: Builder(
            builder: (context) {
              return Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                    child: Text("Sensor"),
                    onPressed: () async {
                      NativeDeviceOrientation orientation =
                      await NativeDeviceOrientationCommunicator().orientation(useSensor: true);
                          content: Text("Native Orientation read: $orientation"),
                          duration: Duration(milliseconds: 500),
                  SizedBox(height: 10),
                    child: Text("UI"),
                    onPressed: () async {
                      NativeDeviceOrientation orientation =
                          await NativeDeviceOrientationCommunicator().orientation(useSensor: false);
                          content: Text("Native Orientation read: $orientation"),
                          duration: Duration(milliseconds: 500),

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  native_device_orientation: ^0.3.0

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:native_device_orientation/native_device_orientation.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health suggestions

Fix lib/native_device_orientation.dart. (-0.50 points)

Analysis of lib/native_device_orientation.dart reported 1 hint:

line 99 col 17: 'inheritFromWidgetOfExactType' is deprecated and shouldn't be used. Use dependOnInheritedWidgetOfExactType instead. This feature was deprecated after v1.12.1..

Maintenance suggestions

The package description is too short. (-4 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8