Flutter:如何防止设备方向变化和强制纵向? [英] Flutter: how to prevent device orientation changes and force portrait?
问题描述
我想阻止我的应用程序改变其方向并强制布局坚持纵向".
I would like to prevent my application from changing its orientation and force the layout to stick to "portrait".
在 main.dart 中,我放了:
In the main.dart, I put:
void main(){
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
runApp(new MyApp());
}
但是当我使用 Android Simulator 旋转按钮时,布局遵循"新的设备方向...
but when I use the Android Simulator rotate buttons, the layout "follows" the new device orientation...
我该如何解决这个问题?
How could I solve this?
谢谢
推荐答案
导入package:flutter/services.dart
,然后
将 SystemChrome.setPreferredOrientations
放在 Widget build()
方法中.
Put the SystemChrome.setPreferredOrientations
inside the Widget build()
method.
示例:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return new MaterialApp(...);
}
}
更新
如 2019 年 10 月更新的 Flutter 文档中所述,此解决方案可能不适用于某些 IOS 设备.
This solution mightn't work for some IOS devices as mentioned in the updated flutter documentation on Oct 2019.
他们建议通过在 Info.plist 中设置 UISupportedInterfaceOrientations 来固定方向
They Advise to fixed the orientation by setting UISupportedInterfaceOrientations in Info.plist like this
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
更多信息https://github.com/flutter/flutter/issues/27235#issuecomment-508995063
这篇关于Flutter:如何防止设备方向变化和强制纵向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!