我希望在我的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"];
我该如何在我的应用程序中实现这个功能?