4

I want to set my app's default orientation as landscape like when I open a game app such as clash of clans or mobile legend. How can I do that in flutter?

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
Rinzin
  • 53
  • 2
  • 7
  • 1
    Welcome to SO. Could you describe your issue in more detail, please? E.g. by adding code, commands or screen shots which describe your problem. Please have also a look to the Help Center, especially for [asking](https://stackoverflow.com/help/how-to-ask) and [minimal examples](https://stackoverflow.com/help/mcve). Thanks. – CKE Sep 17 '18 at 06:51

4 Answers4

4

Put this code in the MyApp()

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight,
  ]);

Like below:

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.landscapeLeft,
        DeviceOrientation.landscapeRight,
      ]);
      return new MaterialApp();
    }
  }

If you want the Portrait mode then check out this answer

ibhavikmakwana
  • 8,948
  • 4
  • 20
  • 39
  • By use of above solution, I think first time App will open with portrait mode and then after changing orientation portrait to landscape it keep landscape. I mean you have to change first time portrait to landscape App will not open up with landscape. – iPatel Aug 26 '19 at 05:54
3

You have to configure iOS and Android individually.

For iOS go to Xcode, select your project > General > Deployment Info > Device orientation and select only the landscape options.

For Android, add android:screenOrientation="landscape" to your <activity> tag.

tudorprodan
  • 950
  • 8
  • 18
  • I found that I needed to do this and what @diegoveloper suggests to force a Flutter app to immediately start and stay in landscape mode only. – Hahnemann Jan 02 '19 at 03:03
1

Use SystemChrome.setPreferredOrientations to set the landscape or portrait mode when you start your main widget.

         void main() { 
          runApp(MyApp()); 
          }

        class MyApp extends StatelessWidget {
          // This widget is the root of your application.
          @override
          Widget build(BuildContext context) {
            config();
            return MaterialApp(
              title: ...
            );
          }

         void config() {
            SystemChrome.setPreferredOrientations([
              DeviceOrientation.landscapeLeft,
              DeviceOrientation.landscapeRight
            ]);
          }
diegoveloper
  • 93,875
  • 20
  • 236
  • 194
  • This worked for me. Make sure you are calling the `config()` method, preferably inside your main method. – Frank Aug 13 '19 at 15:14
0

For Android, setPreferredOrientations works fine.

But for iOs, we have to use this solution (both phone and iPad): https://github.com/flutter/flutter/issues/13238#issuecomment-432981342