I would like to prevent my application from changing its orientation and force the layout to stick to \"portrait\".
In the main.dart, I put:
void mai
Open android/app/src/main/AndroidManifest.xml and add the following line in the MainActivity:
android:screenOrientation="portrait"
If you have this:
<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">
You should end up with something like this:
<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">
This works for Android. On iOS, you will have to change this from the Xcode page: https://i.stack.imgur.com/hswoe.png (as Hejazi said)
Try
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
runApp(MyApp());
}
You can also change the screen orientation settings in the android manifest and ios info.plist file.
Calling SystemChrome.setPreferredOrientations()
doesn't work for me, and I had to change the Device Orientation
in the Xcode project as following:
Set the screenOrientation attribute to portrait
for the main activity in the file android/app/src/main/AndroidManifest.xml
as following:
Just add the following line of code in the main.dart file.
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
and remember to import services.dart file. An example is given below!
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!")),
),
);
}
}
It's possible to enable the requires fullscreen
option in iOS case.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]).then((_) {
runApp(MediaQuery(
data: MediaQueryData(),
child:
MaterialApp(debugShowCheckedModeBanner: false, home: LoginPage())));
});
}
Import import 'package:flutter/services.dart';
Then you include the line of code below in your main.dart file, and in your main method like so:
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitDown,
DeviceOrientation.portraitUp,
]);
runApp(myApp());