I was looking for code which can set orientation of my flutter app landscape forcefully.
How to Detect Orientation Change in Layout In Flutter ?? In order to determine the Orientation of the screen, we can use the OrientationBuilder Widget. The OrientationBuilder will determine the current Orientation and rebuild when the Orientation changes.
The OrientationBuilder calculates the current Orientation by comparing the width and height available to the parent widget, and rebuilds when the size of the parent changes. Using the Orientation , build a list that displays two columns in portrait mode, or three columns in landscape mode.
Enable forcefully
Import package: import 'package:flutter/services.dart';
in main.dart
file
1. Landscape mode:
// Set landscape orientation
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
2. Portrait mode:
// Set portrait orientation
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitDown,
DeviceOrientation.portraitUp,
]);
import 'package:flutter/services.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft])
.then((_) {
runApp(new MyApp());
});
}
SystemChrome.setPreferredOrientations
is applicable for Flutter part of the app, but it's not a full solution. Because when you launching the app - Flutter are not created yet. So you should also setup orientation in the native parts.
Here the article with detailed instructions https://medium.com/@greymag/flutter-orientation-lock-portrait-only-c98910ebd769
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With