Flutter 获取设备方向,修改设备方向

修改屏幕方向。(主动设置+获取系统重力改变回调方法再设置屏幕方向)

一:主动设置:

SystemChrome.setPreferredOrientations([DeviceOrientation.portraitDown,DeviceOrientation.portraitUp])

修改屏幕方向,主动设置。

二:监听获取到系统重力方向改变回调。(从而再设置屏幕方向)

        组件三方:native_device_orientation

        https://pub.dev/packages/native_device_orientation/example

一:主动设置:

设置屏幕方向 :

首先,你需要导入 services 包 :

import  'package:flutter/services.dart';

我们可以通过 SystemChrome 这个类的 setPreferredOrientations方法来设置屏幕方向。

setPreferredOrientations()方法,参数是一个数组 ,我们可以设置多个方向(定义在 DeviceOrientation 枚举类中)。

在Flutter中主函数入口是 main()方法,如果我们想设置整个应用的屏幕方向,在runApp()方法之前设置即可。

void  main()  {
 
 WidgetsFlutterBinding.ensureInitialized();   
 
 SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]).then((_){
 
 runApp(MyApp());
 
 });
 
}

设置屏幕水平方向的方法 :

landscapeLeft or landscapeRight 可以设置一个或者两个,效果试一下就知道了

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

二:监听获取到系统重力方向改变回调。(从而再设置屏幕方向)

  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代码和颤振码之间的通信。

这门课有两种有趣的方法:

  1. Future<NativeDeviceOrientation> orientation(useSensor: false):这可以调用来异步获取方向。

  2. Stream<NativeDeviceOrientation> onOrientationChanged(useSensor: false):这可以调用以获取一个流,该流在方向发生变化时接收新事件。它还应该立即获得初始值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值