This is a plugin project that allows for getting the native device orientation.
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.
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
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
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.
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 properties:
Future<NativeDeviceOrientation> get orientation async:
This can be called to get the orientation asynchronously.
Stream<NativeDeviceOrientation> get onOrientationChanged:
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
Demonstrates how to use the native_device_orientation plugin.
For help getting started with Flutter, view our online documentation.
Add this to your package's pubspec.yaml file:
dependencies: native_device_orientation: ^0.0.4
You can install packages from the command line:
$ flutter packages get
Alternatively, your editor might support
flutter packages get.
Check the docs for your editor to learn more.
Now in your Dart code, you can use:
|0.0.4||Jan 2, 2019|
|0.0.3||Oct 22, 2018|
|0.0.2||Jun 4, 2018|
|0.0.1||Jun 4, 2018|
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]
We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.
flutter format to format
Package is pre-v0.1 release. (-10 points)
While nothing is inherently wrong with versions of
0.0.*, it might mean that the author is still experimenting with the general direction of the API.
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.