欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Flutter 控制屏幕旋转的实现

程序员文章站 2022-06-25 08:43:16
最近需要做个平板的项目,然后需要直接横屏,有2种实现方式。1, 随着屏幕旋转,布局自动调整。做横竖屏适配2,强制屏幕横屏,不随着屏幕去调整第一种方式这里就不做说明了。代码做适配就可以。 下面说一下第二...

最近需要做个平板的项目,然后需要直接横屏,有2种实现方式。

1, 随着屏幕旋转,布局自动调整。做横竖屏适配

2,强制屏幕横屏,不随着屏幕去调整

第一种方式这里就不做说明了。代码做适配就可以。 下面说一下第二种实现方式

flutter 自带方式

flutter 为我们提供了方法来控制系统的横竖屏显示

 systemchrome.setpreferredorientations([
   deviceorientation.landscapeleft,
   deviceorientation.landscaperight,
   deviceorientation.portraitup,
   deviceorientation.portraitdown
  ]).then((_) {
  });

包含的方向类型。 !!!!但是但是但是这个方法只适用于android在ios上没有效果,上网查资料有大神封装的 flutter插件,在ios上都起不到效果,所以打算自己写一个原生文件与flutter进行通讯,实现屏幕旋转。也是笔者在查询资料之后做的一个整合和解释说明

ios端实现屏幕旋转

创建ios原生文件

创建ios原生文件来实现交互,首先要创建个原生文件。命名为flutteriosdeviceplugin。 创建文件

Flutter 控制屏幕旋转的实现

创建一个命名为flutteriosdeviceplugin.h 和命名为flutteriosdeviceplugin.m文件,说明一下: flutteriosdeviceplugin.h文件选择图片里面的header file模块创建即可。

创建之后,一起看一下flutteriosdeviceplugin.h里面的代码

#ifndef flutteriosdeviceplugin_h
#define flutteriosdeviceplugin_h

#import <flutter/flutter.h>

@interface flutteriosdeviceplugin : nsobject<flutterplugin>
+ (void)registerwithregistrar:(nsobject<flutterpluginregistrar>*)registrar flutterviewcontroller:(flutterviewcontroller*) controller;
- (instancetype)newinstance:(nsobject<flutterpluginregistrar>*)registrar flutterviewcontroller:(flutterviewcontroller*) controller;
@end
#endif /* flutteriosdeviceplugin_h */

这个不需要过多说明了看一下flutteriosdeviceplugin.m的代码

#import <foundation/foundation.h>
#import "flutteriosdeviceplugin.h"

@interface flutteriosdeviceplugin () {
  nsobject<flutterpluginregistrar> *_registrar;
  flutterviewcontroller *_controller;
}
@end

static nsstring* const channel_name = @"flutter_ios_device";
static nsstring* const method_change_orientation = @"change_screen_orientation";
static nsstring* const orientation_portrait_up = @"portraitup";
static nsstring* const orientation_portrait_down = @"portraitdown";
static nsstring* const orientation_landscape_left = @"landscapeleft";
static nsstring* const orientation_landscape_right = @"landscaperight";

@implementation flutteriosdeviceplugin
+ (void)registerwithregistrar:(nsobject<flutterpluginregistrar>*)registrar {
  fluttermethodchannel* channel = [fluttermethodchannel
                   methodchannelwithname:channel_name
                   binarymessenger:[registrar messenger]];
  flutteriosdeviceplugin* instance = [[flutteriosdeviceplugin alloc] newinstance:registrar flutterviewcontroller:nil];
  [registrar addmethodcalldelegate:instance channel:channel];
}

+ (void)registerwithregistrar:(nsobject<flutterpluginregistrar>*)registrar flutterviewcontroller:(flutterviewcontroller*) controller {
  fluttermethodchannel* channel = [fluttermethodchannel
                   methodchannelwithname:channel_name
                   binarymessenger:[registrar messenger]];
  flutteriosdeviceplugin* instance = [[flutteriosdeviceplugin alloc] newinstance:registrar flutterviewcontroller:controller];
  [registrar addmethodcalldelegate:instance channel:channel];
}

- (instancetype)newinstance:(nsobject<flutterpluginregistrar>*)registrar flutterviewcontroller:(flutterviewcontroller*) controller{
  _registrar = registrar;
  _controller = controller;
  return self;
}

- (void)handlemethodcall:(fluttermethodcall*)call result:(flutterresult)result {
  if ([method_change_orientation isequaltostring:call.method]) {
    nsarray *arguments = call.arguments;
    nsstring *orientation = arguments[0];
    nsnumber *index = [nsnumber numberwithint: [call.arguments[0] intvalue]];
    nsinteger iosorientation;
     if ([orientation isequaltostring:orientation_landscape_left]){
           iosorientation = uideviceorientationlandscapeleft;
         }else if([orientation isequaltostring:orientation_landscape_right]){
           iosorientation = uideviceorientationlandscaperight;
         }else if ([orientation isequaltostring:orientation_portrait_down]){
           iosorientation = uideviceorientationportraitupsidedown;
         }else{
           iosorientation = uideviceorientationportrait;
         }

    [[uidevice currentdevice] setvalue:@(iosorientation) forkey:@"orientation"];
//    [[nsnotificationcenter defaultcenter] postnotificationname:@"flutteriosdeviceplugin" object:nil userinfo:@{@"orientationmask": index}];
//    [uiviewcontroller attemptrotationtodeviceorientation];

    result(nil);
  } else {
    result(fluttermethodnotimplemented);
  }
}
@end

以上是全部的代码,其中尝试了用通知的方式去做屏幕旋转,后来发现其实没有那么麻烦,直接采用的是

[[uidevice currentdevice] setvalue:@(iosorientation) forkey:@"orientation"];

方式做的。其中遇到的问题:

问题1:iphone手机上可以正常旋转,ipad上不行

这个问题主要原因是info。plist文件里面的iphone和ipad不一致,且勾选requires full screen 即可

Flutter 控制屏幕旋转的实现

回到正轨哈

注册原生插件文件

ios 工程种有个generatedpluginregistrant.m文件,直接注册就可以了。 如果你项目引入了其他第三方插件,也是统一在这个地方注册的

#import "generatedpluginregistrant.h"

#if __has_include(<auto_orientation/autoorientationplugin.h>)
#import <auto_orientation/autoorientationplugin.h>
#else
@import auto_orientation;
#endif

#if __has_include(<orientation/orientationplugin.h>)
#import <orientation/orientationplugin.h>
#else
@import orientation;
#endif
#import "flutteriosdeviceplugin.h"

@implementation generatedpluginregistrant

+ (void)registerwithregistry:(nsobject<flutterpluginregistry>*)registry {
 [autoorientationplugin registerwithregistrar:[registry registrarforplugin:@"autoorientationplugin"]];
 [orientationplugin registerwithregistrar:[registry registrarforplugin:@"orientationplugin"]];
//  [registry registrarforplugin:@"flutteriosdeviceplugin"];
  

  [flutteriosdeviceplugin registerwithregistrar: [registry registrarforplugin:@"flutteriosdeviceplugin"]];
}

@end

代码中的这个片段是自己写的插件注册的方法,其他的autoorientationplugin,orientationplugin是引用第三方插件自动生成的代码。

[flutteriosdeviceplugin registerwithregistrar: [registry registrarforplugin:@"flutteriosdeviceplugin"]];

appdelegate文件中的设置

import uikit
import flutter



@uiapplicationmain
@objc class appdelegate: flutterappdelegate {
 var orientationmask: uiinterfaceorientationmask = .all;
  
 override func application(
  _ application: uiapplication,
  didfinishlaunchingwithoptions launchoptions: [uiapplication.launchoptionskey: any]?
 ) -> bool {
  notificationcenter.default.addobserver(self, selector: #selector(changelandscape(center:)), name:nsnotification.name(rawvalue: "flutteriosdeviceplugin"), object: nil)
  generatedpluginregistrant.register(with: self);
  return super.application(application, didfinishlaunchingwithoptions: launchoptions)
 }
  override func application(_ application: uiapplication, supportedinterfaceorientationsfor window: uiwindow?) -> uiinterfaceorientationmask {
    return orientationmask;
  }
  
  @objc func changelandscape(center: notification){
    let index: nsnumber = (center.userinfo?["orientationmask"] ?? 5) as! nsnumber
    var mask : uiinterfaceorientationmask = .all
    switch index {
    case 0:
      mask = .portrait
      break
    case 1:
      mask = .landscapeleft
      break
    case 2:
      mask = .landscaperight
      break
    case 3:
      mask = .portraitupsidedown
      break
    case 4:
      mask = .landscape
      break
    case 5:
      mask = .all
      break
    case 6:
      mask = .allbutupsidedown
      break
    
    default:
      mask = .all
      break
    }
    orientationmask = mask;
    _ = application(uiapplication.shared, supportedinterfaceorientationsfor: uiapplication.shared.keywindow)
  }
}

其中changelandscape方法是控制监听的事件的方法,目前没什么用。 主要代码是这一段,这个是在发出屏幕旋转的时候回调的方法,这里面设置的是全部的方向都可以。

  override func application(_ application: uiapplication, supportedinterfaceorientationsfor window: uiwindow?) -> uiinterfaceorientationmask {
    return orientationmask;
  }

问题2:程序不走该方法

具体原因是因为info.plist文件问题。参考上面设置就没问题

flutter 原生代码使用

这个地方是针对于ios平台去做的区别。

methodchannel _channel = const methodchannel('flutter_ios_device');

@override
 void initstate() {
  systemchrome.setpreferredorientations([
   deviceorientation.landscapeleft,
   deviceorientation.landscaperight,
   deviceorientation.portraitup,
   deviceorientation.portraitdown
  ]).then((_) {
   if (platform.isios) {
    this.changescreenorientation(deviceorientation.landscapeleft);
   }
  });
  super.initstate();
 }
 
future<void> changescreenorientation(deviceorientation orientation) {
  string o;
  switch (orientation) {
   case deviceorientation.portraitup:
    o = 'portraitup';
    break;
   case deviceorientation.portraitdown:
    o = 'portraitdown';
    break;
   case deviceorientation.landscapeleft:
    o = 'landscapeleft';
    break;
   case deviceorientation.landscaperight:
    o = 'landscaperight';
    break;
  }
  return _channel.invokemethod('change_screen_orientation', [o]);
 }

代码不用过多说明了, 有不了解的可以留言

问题3: 启动程序自动旋转

systemchrome.setpreferredorientations需要设置全部内容。不然会默认旋转。有点小坑,

以上基本上可以实现屏幕旋转的问题,如果有小伙伴还是不行,可以试一下用ios原生通知的方式去完成。这个我做的是平板的项目,暂时没有问题,更多相关flutter 控制屏幕旋转内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!