I was looking for code which can set orientation of my flutter app landscape forcefully.
import 'package:flutter/services.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft])
.then((_) {
runApp(new MyApp());
});
}
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,
]);
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