[英]How to force flutter device orientation for iOS
I wanna force the orientation of some pages of my application.我想强制我的应用程序的某些页面的方向。 Like, I want the orientation to be Portrait for the landing page, the options of my game, and the player selection, but I want to force the orientation to be landscape when the game begins... I tried using:
就像,我希望登录页面的方向为纵向,我的游戏选项和玩家选择,但我想在游戏开始时强制方向为横向......我尝试使用:
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
])
and it does work on android, (except that it change the orientation 3 times before getting the right orientation...) But on iOS, it doesn't force the change... It only change when you turn the device and then it works and stays in landscape mode.它确实适用于 android,(除了它在获得正确方向之前改变方向 3 次......)但在 iOS 上,它不会强制改变......它只会在你转动设备然后它时改变工作并保持横向模式。
Does anyone had the same issue and know how to fix it?有没有人有同样的问题并且知道如何解决它?
I tried to put the SystemChrome.setPreferredOrientations
in the Widget build function but it didn't worked.我试图将
SystemChrome.setPreferredOrientations
放入 Widget 构建 function 但它没有用。
Please try this.请试试这个。
void main() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(new MyApp());
});
}
2nd Way:第二种方式:
await SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight
]);
The list of supported orientations are:支持的方向列表是:
landscapeLeft
landscapeRight
portraitDown
portraitUp
Import package:flutter/services.dart
, then导入
package:flutter/services.dart
,然后
Put the SystemChrome.setPreferredOrientations
inside the Widget build() method.将
SystemChrome.setPreferredOrientations
放入 Widget build() 方法中。
Example:例子:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return new MaterialApp(...);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.