Search code examples
flutterflutter-layout

Flutter how to disable landscape orientation?


I want to disable the landscape mode. I tried to allow only portrait mode using following code. But it is not working with my physical device. How to solve this?

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  runApp(MyApp());
}

Solution

  • You need to paste the code in Widget build(). Consider this answer for more details

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