How to set my app to landscape mode in Flutter?

Viewed 3351

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?

4 Answers

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

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.

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
            ]);
          }
Related