画面の向きを変更します。(アクティブ設定 + システムの重力変更コールバック メソッドを取得し、画面の向きを設定します)
1: アクティブ設定:
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitDown,DeviceOrientation.portraitUp])
画面の向きを変更し、自動的に設定します。
2: システムの重力方向の変化を監視し、コールバックを取得します。(これにより画面の向きがリセットされます)
コンポーネントの 3 部構成:native_device_orientation
https://pub.dev/packages/native_device_orientation/example
1: アクティブ設定:
画面の向きを設定します。
まず、サービス パッケージをインポートする必要があります。
import 'package:flutter/services.dart';
SystemChrome クラスの setPreferredOrientations メソッドを通じて画面の向きを設定できます。
setPreferredOrientations() メソッドの場合、パラメーターは配列であり、複数の方向を設定できます (DeviceOrientation 列挙クラスで定義されています)。
Flutter のメイン関数エントリは main() メソッドで、アプリケーション全体の画面の向きを設定したい場合は、runApp() メソッドの前に設定できます。
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]).then((_){
runApp(MyApp());
});
}
画面の横方向を設定する方法:
LandscapeLeft または LandscapeRight は 1 つまたは 2 つを設定できます。試してみると効果がわかります
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft,DeviceOrientation.landscapeRight])
画面の垂直方向を設定します。
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitDown,DeviceOrientation.portraitUp])
画面の向きを動的に変更します。
RaisedButton(
child: Text("Portrait"),
onPressed: (){
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
})
現在の画面の向きを取得します。
MediaQuery.of(context).orientation
MediaQuery.of(context).orientation == Orientation.landscape
2: システムの重力方向の変化を監視し、コールバックを取得します。(これにより画面の向きがリセットされます)
void listenOrientation() async {
NativeDeviceOrientationCommunicator()
.onOrientationChanged(useSensor: true)
.listen((event) async {
if (!mounted ||
!_isAutoOrientation ||
_isActive == false ||
isWorkBook
) return;
NativeDeviceOrientation nativeDeviceOrientation = event;
currentOrientation = await NativeDeviceOrientationCommunicator()
.orientation(useSensor: true);
timeOfLastChange = DateTime.now();
Future.delayed(const Duration(milliseconds: 600), () async {
NativeDeviceOrientation currentOrientation1 =
await NativeDeviceOrientationCommunicator()
.orientation(useSensor: true);
if (DateTime.now().difference(timeOfLastChange).inMilliseconds >= 600 &&
currentOrientation1 == currentOrientation) {
stableOrientation = currentOrientation1;
switch (stableOrientation) {
case NativeDeviceOrientation.portraitUp:
if (VideoPlayPage.isManualFullScreen) {
return;
}
setPortraitUp();
break;
case NativeDeviceOrientation.landscapeLeft:
setLandscape();
VideoPlayPage.isManualFullScreen = false;
break;
case NativeDeviceOrientation.landscapeRight:
setLandscape();
VideoPlayPage.isManualFullScreen = false;
break;
}
}
});
});
}
Native_device_orientation: https://pub.dev/packages/native_device_orientation
オリジナル: ヘルパー ウィジェットをバイパスして、ネイティブ呼び出しに直接アクセスすることも可能です。これはクラスを使用してNativeDeviceOrientationCommunicator
行われます。これはシングルトンですが、通常のクラスと同様にインスタンス化でき、iOS/Android コードと Flutter コード間の通信を処理します。
このクラスには 2 つの興味深いメソッドがあります。
-
Future<NativeDeviceOrientation> orientation(useSensor: false)
: これを呼び出して、非同期で道順を取得できます。 -
Stream<NativeDeviceOrientation> onOrientationChanged(useSensor: false)
: これを呼び出して、向きが変わったときに新しいイベントを受け取るストリームを取得できます。また、初期値をすぐに取得する必要があります。