flutter

Prevent device rotation in Flutter

If you've ever used an app on your phone and rotated your device, only to have the app rotate with it, then you know how annoying it can be. Luckily, there is a way to prevent this from happening in Flutter. By using the following code, you can ensure that your app will remain in the orientation that you originally intended it to be in.

import 'package:flutter/services.dart'

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

Flutter is a mobile app SDK that allows you to write code once and deploy to both iOS and Android. One of the advantages of Flutter is that it allows you to easily control the look and feel of your app across both platforms.

One thing that can be difficult to control is the device rotation. By default, most devices will rotate the screen when the device is rotated. This can be annoying for users and can cause your app to look broken if not properly handled.

Fortunately, Flutter makes it easy to prevent device rotation. In this article, we'll show you how to quickly add a setting to your app that will disable screen rotation.

Was this helpful?