DISCOVER FLUTTER — WEEK #22
Change Screen Orientation In Flutter
Screen orientation is very important from the user’s point of view, especially when it comes to mobile games. In that case, you may need to set the orientation of the application to fix direction.
In this article, we will learn how to set the screen orientation and how to extract information about which type of orientation is currently set.
Setting Screen Orientation
- Import the services package.
import 'package:flutter/services.dart';
2. Change the main()
method, and apply the following code:
void main() {
WidgetsFlutterBinding.ensureInitialized(); SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft]).then((_){
runApp(MyApp());
});
}
setPrefferedOrientations
of the SystemChrome
class is the key method in which you can set preferred orientation. For this method, we need to use then
because its return type is Future
object.
Note: If you need the binding to be initialized before the runApp
method, you need to call the ensureInitialized()
method of the WidgetsFlutterBinding
class.