Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set landscape orientation mode for flutter app?

I was looking for code which can set orientation of my flutter app landscape forcefully.

like image 307
Ashvin A Avatar asked Aug 12 '18 07:08

Ashvin A


People also ask

How do you get the orientation on device Flutter?

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.

What is orientation Flutter?

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.


3 Answers

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,
]);

enter image description here

2. Portrait mode:

// Set portrait orientation
SystemChrome.setPreferredOrientations([
   DeviceOrientation.portraitDown,
   DeviceOrientation.portraitUp,
]);

Set portrait mode

like image 183
Ashvin A Avatar answered Oct 22 '22 11:10

Ashvin A


import 'package:flutter/services.dart';

void main()  {
   WidgetsFlutterBinding.ensureInitialized();
   SystemChrome.setPreferredOrientations([DeviceOrientation.landscapeLeft])
   .then((_) {
    runApp(new MyApp());
  });
}
like image 24
mottu Avatar answered Oct 22 '22 11:10

mottu


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

like image 6
greymag Avatar answered Oct 22 '22 13:10

greymag