skip to Main Content

In flutter how would I disable landscape orientation in Mobile below 700 pixels width only? For any other device above this they are allowed to use landscape or portrait orientation.

Thanks

2

Answers


  1. I had a similar application and made a class for it. You just need to init it from your first screen and it will do the job. And it need to called from the build method cause it needs context to find the screen size with MeditaQuery

    here’s the code:

    class DeviceOrientationConfig {
      void init(BuildContext context) {
    
        MediaQueryData mediaQueryData = MediaQuery.of(context);
    
        // If the device's width is greater than 700.
        if (mediaQueryData.size.width > 700) {
          // device is tablet.
          AppSingleton.isMobileDevice = false;
          SystemChrome.setPreferredOrientations(
              [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
        }
        // Device's width is less than 700.
        else {
          // device is mobile.
          AppSingleton.isMobileDevice = true;
          SystemChrome.setPreferredOrientations(
              [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
        }
      }
    }
    

    I needed the check if it’s mobile or tablet for other uses so i’ve taken the isMobileDevice in AppSingleton you can remove it.

    Login or Signup to reply.
  2. class MyApp extends StatelessWidget {
    
      MyApp({Key key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        if (MediaQuery.of(context).size.width < 600) {
          SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
        //Write your code here
        }else{
           //write your other code if needed
        }
        return MaterialApp(
          home: MyHomePage(),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search