我想阻止我的应用程序改变其方向,并迫使布局坚持“纵向”。
大体上是这样。省道,我放:
void main(){
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
runApp(new MyApp());
}
但当我使用Android模拟器旋转按钮时,布局“遵循”新的设备方向…
我怎么解决这个问题呢?
谢谢
首先主要导入这一点。飞镖文件
import 'package:flutter/services.dart';
然后不要复制粘贴,而是看(记住)和写下面的代码在主要。飞镖文件
强制进入纵向模式:
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp,DeviceOrientation.portraitDown])
.then((_) => runApp(MyApp()),
);
在横屏模式下强制:
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft,DeviceOrientation.landscapeRight])
.then((_) => runApp(MyApp()),
);
android有两个选项
1. 写在主
main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(
child: MyApp(),
);
});
}
2. 在AndroidManifest.xml文件中设置
iOS上也有两种选择
1. 从info.plist
将这一行添加到您的信息中。plist文件
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
2. 从跑步者
打开你的奔跑者。Xcode的iOS文件夹中的xcworkspace。点击Runner而不是Pods。您可以在“常规>部署信息”上找到此选项。看看你想要什么
首先主要导入这一点。飞镖文件
import 'package:flutter/services.dart';
然后不要复制粘贴,而是看(记住)和写下面的代码在主要。飞镖文件
强制进入纵向模式:
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp,DeviceOrientation.portraitDown])
.then((_) => runApp(MyApp()),
);
在横屏模式下强制:
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft,DeviceOrientation.landscapeRight])
.then((_) => runApp(MyApp()),
);