Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 142 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flutter: how to prevent device orientation changes and force portrait?

#1
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 main(){
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
runApp(new MyApp());
}

but when I use the Android Simulator rotate buttons, the layout "follows" the new device orientation...

How could I solve this?

Thanks
Reply

#2
The 'setPreferredOrientations' method returns a Future object.
Per documentation a Future represents some value that will be available somewhere in future. That's why you shall wait until it's available and then move on with the application. Hence, there shall be used 'then' method, which, per definition, "registers callbacks to be called when the Future completes". Hence, you shall use this code:

void main() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
runApp(new App());
});
}

Also, the following file must be imported:

'package:flutter/services.dart'
Reply

#3
`setPreferredOrientation` returns a `Future<void>`, so it is asynchronous. The most readable approach is to define `main` as asynchronous:

```dart
Future<void> main() async {
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
return runApp(new MyApp());
}
```

Reply

#4
Import `package:flutter/services.dart`, then

Put the `SystemChrome.setPreferredOrientations` inside the `Widget build()` method.

Example:

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return new MaterialApp(...);
}
}

**Update**

This solution mightn't work for some IOS devices as mentioned in the updated flutter documentation on Oct 2019.

They Advise to fixed the orientation by setting UISupportedInterfaceOrientations in Info.plist like this

<array>
<string>UIInterfaceOrientationPortrait</string>
</array>

For more information

[To see links please register here]

Reply

#5
First of all import this in **main.dart** file

import 'package:flutter/services.dart';

Then **don't copy paste** rather see(remember) and write below code in **main.dart** file

To force in **portrait** mode:

void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp,DeviceOrientation.portraitDown])
.then((_) => runApp(MyApp()),
);

To force in **landscape** mode:


void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.landscapeLeft,DeviceOrientation.landscapeRight])
.then((_) => runApp(MyApp()),
);

Reply

#6
### iOS:
Calling `SystemChrome.setPreferredOrientations()` doesn't work for me, and I had to change the `Device Orientation` in the [Xcode project][1] as following:

[![enter image description here][2]][2]


### Android:
Set the [`screenOrientation`][3] attribute to `portrait` for the main activity in the file `android/app/src/main/AndroidManifest.xml` as following:

[![enter image description here][4]][4]


[1]:

[To see links please register here]

[2]:

[3]:

[To see links please register here]

[4]:
Reply

#7
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">
</code>

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:
(as Hejazi said)
Reply

#8
As of new flutter versions along with setting the `preferred Orientation` we need to add one extra line i.e

WidgetsFlutterBinding.ensureInitialized();

So working code for this is -



import 'package:flutter/services.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown
]);
runApp(MyApp());
}
Reply

#9
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.
Reply

#10
Below is the official example of the flutter team.

[To see links please register here]


import 'package:flutter/services.dart' show DeviceOrientation, SystemChrome;

void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
runApp(HomeScreen());
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through