UI 방향 또는 하드웨어 센서에서 데이터를 가져오는 기기의 네이티브 방향에 액세스할 수 있는 플러터 플러그인입니다. 이 플러그인은 안드로이드 및 iOS 플랫폼에서 기기 회전 변화를 감지하고 반응할 수 있도록 해줍니다. 증강 현실, 게임, 카메라 기반 기능과 같은 정밀한 방향 인식이 필요한 애플리케이션에 유용합니다. 플러터의 이벤트 시스템과 원활하게 통합되어 기기 방향이 변경될 때 실시간으로 업데이트됩니다.
사용 사례
증강현실 애플리케이션
모션 컨트롤로 게임하기
카메라 앱 방향 처리
장치 회전에 기반한 반응형 사용자 인터페이스
센서 기반의 상호작용 경험
주요 기능
실시간 방향 업데이트
UI 및 센서 기반 방향 둘 다 지원
크로스 플랫폼 호환성 (Android 및 iOS)
간단한 이벤트 기반 API
정확한 장치 회전 감지
적합한 대상
정밀한 장치 방향이 필요한 앱
AR 또는 VR 경험을 구축하는 개발자
운동 입력이 필요한 게임
동적 레이아웃 조정을 갖춘 애플리케이션
고려 사항
Android에서 적절한 권한이 필요합니다
지속적으로 모니터링될 경우 추가 배터리 소모가 발생할 수 있습니다
기기 수준의 방향 데이터만 제한됨
고급 센서 융합 요구 사항에 적합하지 않습니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Native Device Orientation
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 isrequired
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:
Future<NativeDeviceOrientation> orientation(useSensor: false):
This can be called to get the orientation asynchronously.
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.