搜尋套件、儲存庫與 APP 分類

套件詳情v2.1.3

native_device_orientation

一個用於從 UI 方向或感應器讀取設備原生方向的 Flutter 插件

AndroidiOS
近 30 天下載
17.6萬
喜歡數
150
Pub 分數
160

安裝套件

flutter pub add native_device_orientation

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 插件,提供對裝置原生方向的存取,資料可從 UI 方向或硬體感測器取得。此插件讓應用程式能夠偵測並回應 Android 與 iOS 平台上的裝置旋轉變更。對於需要精確方向感知的應用程式(例如擴增實境、遊戲或相機功能)極為有用。它與 Flutter 的事件系統無縫整合,可在裝置方向變更時即時更新。

適用情境

  • 增強現實應用
  • 使用體感控制玩遊戲
  • 相機應用方向處理
  • 基於設備旋轉的響應式使用者介面
  • 基於傳感器的互動體驗

主要特色

  • 即時方向更新
  • 同時支援基於UI和感應器的方向
  • 跨平台相容性(Android 與 iOS)
  • 簡單的事件驅動 API
  • 精確的設備旋轉檢測

適合對象

  • 需要精確設備方向的應用
  • 開發AR或VR體驗的開發者
  • 需要運動輸入的遊戲
  • 具有動態佈局調整的應用程式

使用建議

  • 需要在 Android 上具有適當的權限
  • 持續監控時可能額外消耗電池電量
  • 僅限設備級別的方向資料
  • 不適合高階感測器融合需求

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Native Device Orientation

pub package

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

NOTE for Swift Package Manager

After switching to Swift Package Manager, if you're going to run the example you have to make sure that the folder name of the entire repository is "native_device_orientation" rather than the default of flutter_native_device_orientation from when you do a clone. This is because in their infinite wisdom, Apple made the directory name the root source of naming rather than the Package.swift or something else.

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.

Updating to 2.*

Due to the objective c code being removed from the project, updating to 2.0 can sometimes result in a build error, something like the following:

Definition of 'NativeDeviceOrientationPlugin' must be imported from module 'nativeDeviceOrientation.Swift' before it is required

Unfortunately, this is a build-system issue and as such cannot be fixed easily by the developer. To resolve the issue, you can simply follow these steps:

  • Update to flutter_native_device_orientation version 2.*
  • remove your Podfile.lock
  • run flutter clean
  • run flutter build ios or run for an iOS device

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 "reader" widget

There are three ways of using the plugin. The simplest 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 the source code for more details.

Using the plugin - built-in "oriented" widget

The second approach is more involved, but involves slightly less boilerplate, and may be a bit more obvious to use. It wraps the NativeDeviceOrientationReader widget, then automatically checks the orientation for you. Instead of passing in a single builder function, you pass one for each orientation you wish to define: landscapeLeft, landscapeRight, portraitUp, and portaitDown are the most obvious, followed by simply landscape and portrait for situations where you don't care to define different layouts for either of those, and finally the (required) fallback, used in cases where either you don't have a more specific builder defined, or something goes horribly horribly wrong.

You sacrifice a bit of control over when to actually retrieve the orientation info in exchange for the plugin handling orientation updates automatically. Since this approach does a bit more hand holding, processing things like NativeDeviceOrientation.unknown for you, it's the approach in the example app.

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.

所屬分類

依賴

依賴清單 1 項

  • plugin_platform_interface^2.0.2

原始碼儲存庫

使用此套件的專案