DISCOVER FLUTTER — WEEK #22

Change Screen Orientation In Flutter

Jelena Jovanoski
2 min readJan 26, 2021
Photo by eberhard grossgasteiger on Unsplash

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

  1. 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.

Setting to Portrait Mode Only

--

--

Jelena Jovanoski
Jelena Jovanoski

Written by Jelena Jovanoski

On my journey to become a Flutter dev I will be sharing knowledge by writing short texts about what new know-how’s I’ve learned, in the next 30 weeks.