ios - iOS 上单页的 Flutter 设备方向
问题描述
我想在我的 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 提到了如何解决这个问题。
“我解决了创建一个小型平台通道的问题,该通道在调用 setPreferredOrientations 之前调用此代码以切换到纵向:”
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationPortrait) forKey:@"orientation"];
以及切换到横向的对应代码
[[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeLeft) forKey:@"orientation"];
如何在我的应用程序中实现这一点?
解决方案
我在我的一个应用程序中有完全相同的要求。你很幸运——整个项目都是开源的!让我们完成它:
- 在 iOS 端添加平台通道逻辑
ios/Runner/AppDelegate.m
- 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
- 在应该面向横向的小部件/屏幕中定义
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 部分(没有这样的通道,因为没有它它可以按预期工作)。
- 处理时 - 旋转回纵向模式 - 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
为更适合您的项目的东西:)
推荐阅读
- c# - C# 代码输出无穷大并给出错误答案
- python - 在 >Python 3.7 中使用 importlib(或其他东西)复制 imp.load_dynamic?
- perl - 找不到有效的 termcap 文件
- callback - 如何在回调请求中动态发送json正文
- extjs3 - 编辑 Extjs 3.4 时无法检查 xchekbox modx
- python - 如何嵌入响应?不和谐机器人 Python
- python - Spring_layout 不适用于权重
- javascript - 如何将翻译添加到我的 matrix3d 旋转 3D CSS 立方体
- apache-kafka - 在spring app prometheus中启用kafka客户端指标
- image-processing - 初学者如何分析粒子?