我想阻止我的应用程序更改其方向并强制布局坚持"肖像".
在main.dart中,我把:
void main(){
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
runApp(new MyApp());
}
Run Code Online (Sandbox Code Playgroud)
但是当我使用Android模拟器旋转按钮时,布局"跟随"新的设备方向......
我该怎么解决这个问题?
谢谢
Mas*_*son 96
把方法放在package:flutter/services.dart
里面SystemChrome.setPreferredOrientations
.
例:
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return new MaterialApp(...);
}
}
Run Code Online (Sandbox Code Playgroud)
Abe*_*bal 41
打开 android/app/src/main/AndroidManifest.xml 并在 MainActivity 中添加以下行:
android:screenOrientation="portrait"
Run Code Online (Sandbox Code Playgroud)
如果你有这个:
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
Run Code Online (Sandbox Code Playgroud)
你应该得到这样的结果:
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustResize">
Run Code Online (Sandbox Code Playgroud)
这适用于Android。在 iOS 上,您必须从 Xcode 页面更改此设置:https ://i.stack.imgur.com/hswoe.png(如 Hejazi 所说)
Tej*_*oid 35
@boeledi,如果你想"锁定"设备方向而不允许它随着用户旋转手机而改变,这很容易设置如下,
// This did not work as requirement
void main() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
runApp(new MyApp());
}
Run Code Online (Sandbox Code Playgroud)
你必须等到
setPreferredOrientations
完成然后启动应用程序
// This will works always for lock screen Orientation.
void main() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(new MyApp());
});
}
Run Code Online (Sandbox Code Playgroud)
小智 34
把WidgetsFlutterBinding.ensureInitialized()
别人同时建立,你会得到一个错误。
import 'package:flutter/services.dart';
void main() async => {
WidgetsFlutterBinding.ensureInitialized();
await SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp],
); // To turn off landscape mode
runApp(MainApp());
};
Run Code Online (Sandbox Code Playgroud)
Pin*_*rji 16
首先在main.dart文件中导入
import 'package:flutter/services.dart';
Run Code Online (Sandbox Code Playgroud)
然后不要复制粘贴而是查看(记住)并在main.dart文件中写入以下代码
要强制使用纵向模式:
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp,DeviceOrientation.portraitDown])
.then((_) => runApp(MyApp()),
);
Run Code Online (Sandbox Code Playgroud)
强制横向模式:
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft,DeviceOrientation.landscapeRight])
.then((_) => runApp(MyApp()),
);
Run Code Online (Sandbox Code Playgroud)
Ara*_*yan 13
setPreferredOrientations方法返回一个Future对象。每个文档中,“ Future”表示将来将在某处可用的某些值。这就是为什么您要等到它可用后再继续使用该应用程序。因此,应使用'then'方法,根据定义,该方法“注册在Future完成时要调用的回调”。因此,您应使用以下代码:
void main() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
runApp(new App());
});
}
Run Code Online (Sandbox Code Playgroud)
另外,必须导入以下文件:
``package:flutter / services.dart''
Ana*_*nan 13
颤动方向锁定:仅限纵向
\n在 Flutter 中,我们有 SystemChrome.setPreferredOrientation() (请参阅文档)来定义首选方向。您可以在所有有关 Flutter 中设置方向的文章中找到这种方法。让\xe2\x80\x99s 看看我们如何使用它:
\nimport \'package:flutter/material.dart\';\nimport \'package:flutter/services.dart\';\n\nvoid main() {\n // We need to call it manually,\n // because we going to call setPreferredOrientations()\n // before the runApp() call\n WidgetsFlutterBinding.ensureInitialized();\n \n // Than we setup preferred orientations,\n // and only after it finished we run our app\n SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])\n .then((value) => runApp(MyApp()));\n}\n
Run Code Online (Sandbox Code Playgroud)\niOS系统
\n在 Xcode 中打开项目,在项目导航器中(ios/Runner.xcworkspace)
选择,选择 Target并在 D 部分的选项卡上Runner
Runner
General
eployment Info
选项卡上我们可以设置设备方向:
另外,我们可以手动完成,而无需打开 Xcode \xe2\x80\x94 只需编辑ios/Runner/Info.plist
. 将其作为文本文件打开,找到密钥UISupportedInterfaceOrientation
并仅保留所需的值:
<key>UISupportedInterfaceOrientations</key>\n<array>\n <string>UIInterfaceOrientationPortrait</string>\n</array>\n
Run Code Online (Sandbox Code Playgroud)\n安卓
\n要定义方向,Android
我们需要编辑ApplicationManifest
. 打开android/app/src/main/AndroidManifest.xml
并添加属性 screenOrientationmain
:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"\n package="com.example.orientation_lock_example">\n <application ...>\n <activity\n android:name=".MainActivity"\n android:screenOrientation="portrait"\n ...\n />\n ... \n </activity>\n ... \n </application>\n</manifest>\n
Run Code Online (Sandbox Code Playgroud)\n这就是\xe2\x80\x99 的全部。这里是带有示例应用程序的存储库:
\n希望这有帮助。
\n小智 9
进口import 'package:flutter/services.dart';
然后将下面的代码行包含在您的main.dart
文件和 main 方法中,如下所示:
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitDown,
DeviceOrientation.portraitUp,
]);
runApp(myApp());
Run Code Online (Sandbox Code Playgroud)
对于人们来说,他们现在正在阅读这个问题。我找到的最简单的方法,它适用于 android 和 ios 设备。
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations(
[
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
],
).then((val) {
runApp(YourAppName());
});
}
Run Code Online (Sandbox Code Playgroud)
对于 Android,您有两种选择
1. 在main上写
main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(
child: MyApp(),
);
});
}
Run Code Online (Sandbox Code Playgroud)
对于 iOS,您还有两种选择
1. 来自info.plist
将此行添加到您的info.plist
文件中
<array>
<string>UIInterfaceOrientationPortrait</string>
</array>
Run Code Online (Sandbox Code Playgroud)
2. 从 Runner
打开Runner.xcworkspace
Xcode 上的 iOS 文件夹。单击“Runner”而不是“Pod”。您可以在“常规”>“部署信息”中找到此选项。只需检查你想要什么
setPreferredOrientation
返回 a Future<void>
,所以它是异步的。最易读的方法是定义main
为异步:
Future<void> main() async {
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
return runApp(new MyApp());
}
Run Code Online (Sandbox Code Playgroud)
小智 5
只需在 main.dart 文件中添加以下代码行。
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
Run Code Online (Sandbox Code Playgroud)
并记得导入 services.dart 文件。下面给出一个例子!
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
return MaterialApp(
home: Scaffold(
body: Center(child: Text("A Flutter Example!")),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
24238 次 |
最近记录: |