ios - iOS 上单页的 Flutter 设备方向

标签 ios flutter landscape landscape-portrait

我想在我的 Flutter 应用程序中以横向模式显示单个页面。其他所有屏幕都应以纵向模式显示。

我找到了这段代码:

在我的 main.dart 中

SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]).then((_) {
    runApp(new IHGApp());
  });

这会以纵向模式启动应用程序。所以我有了想要在横向模式下显示的屏幕,这是我在那里使用的代码:

@override
  void initState() {
    super.initState();
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
    ]);
  }


  @override
  void dispose() {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    super.dispose();
  }

这适用于 Android。

在 iOS 上,似乎无法为单个页面强制使用横向模式。

https://github.com/flutter/flutter/issues/13238

在这篇文章中,我发现了这个问题的问题。 sroddy 提到了如何解决这个问题。

“我解决了创建一个小型平台 channel 的问题,该 channel 在调用 setPreferredOrientations 之前调用此代码以切换到纵向:”

[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];

以及切换到横向的对应代码

[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];

如何在我的应用中实现这一点?

最佳答案

我的一个应用程序也有完全相同的要求。你很幸运——整个项目都是开源的!让我们完成它:

  1. ios/Runner/AppDelegate.m中添加iOS端平台 channel 逻辑- https://github.com/vintage/party_flutter/blob/27b11fc46755d8901f02c3b439b294ca9005277a/ios/Runner/AppDelegate.m#L8-L23
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;

    FlutterMethodChannel* rotationChannel = [FlutterMethodChannel
                                             methodChannelWithName:@"zgadula/orientation"
                                             binaryMessenger:controller];

    [rotationChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
        if ([@"setLandscape" isEqualToString:call.method]) {
            [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
        }
        else if ([@"setPortrait" isEqualToString:call.method]) {
            [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
        }
        else {
            result(FlutterMethodNotImplemented);
        }
    }];

    [GeneratedPluginRegistrant registerWithRegistry:self];
    // Override point for customization after application launch.
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

@end
  1. 在应该是横向的小部件/屏幕中定义 MethodChannel - https://github.com/vintage/party_flutter/blob/master/lib/ui/screens/game_play.dart#L34
static const _rotationChannel = const MethodChannel('zgadula/orientation');

3。 initState应该触发横向旋转 - https://github.com/vintage/party_flutter/blob/master/lib/ui/screens/game_play.dart#L71-L78

SystemChrome.setPreferredOrientations([
  DeviceOrientation.landscapeRight,
]);
// TODO: Remove it when fixed in Flutter
// https://github.com/flutter/flutter/issues/13238
try {
  _rotationChannel.invokeMethod('setLandscape');
} catch (error) {}

try-catch 是处理 Android 部分(没有这样的 channel ,因为没有它它可以按预期工作)。

  1. 处理时 - 旋转回纵向模式 - https://github.com/vintage/party_flutter/blob/master/lib/ui/screens/game_play.dart#L111-L122

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

// TODO: Remove it when fixed in Flutter
// https://github.com/flutter/flutter/issues/13238
try {
  _rotationChannel.invokeMethod('setPortrait');
} catch (error) {}

if (_rotateSubscription != null) {
  _rotateSubscription.cancel();
}

随意更改zgadula/orientation更适合您的项目的东西:)

关于ios - iOS 上单页的 Flutter 设备方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52979080/

相关文章:

ios - 强制一个 View Controller 只有横向

ios - 为 iOS 共享自定义 URL

ios - iOS 7 中的 UISwitch 显示边框

ios - AWS iOS v2 Cognito 与 STS 身份验证

firebase - 名称为 DEFAULT 的 Flutter Firebase 实时数据库 FirebaseApp 不存在。可用的应用程序名称 : [DEFAULT]

Flutter:如何制作一个始终滚动到底部的列表?

android - 默认情况下如何在横向全屏模式下播放youtube数据API视频

Android 在横向工具栏内强制 ActionBar

iOS Swift 如何告诉 UIView Controller 我们通过 NSURLProtocol 对象接收到信息

flutter - Flutter中的自定义按钮