How to Lock Mobile Phone Screen Orientation In Flutter? (Not in Tabs/ iPads)

101
import 'package:flutter/services.dart'; 
       
       //When you load the Widget, do something like this:
       
       @override
       void initState(){
         super.initState();
        if(getDeviceType == ‘phone’){    
             SystemChrome.setPreferredOrientations([
                DeviceOrientation. portraitUp,
             ]);    
        }    
        else{
            SystemChrome.setPreferredOrientations([    
                DeviceOrientation. portraitUp,
                DeviceOrientation.landscapeRight,
                DeviceOrientation.landscapeLeft,
          ]);
        }
       }
       
       
       String getDeviceType() {
           final data = MediaQueryData.fromWindow(WidgetsBinding.instance.window);
           return data.size.shortestSide < 600 ? 'phone' :'tablet';
         }
Share:
101
Chanaka Niroshan
Author by

Chanaka Niroshan

Updated on January 01, 2023

Comments

  • Chanaka Niroshan
    Chanaka Niroshan over 1 year

    I have developed an app using flutter. Now I want to lock it's screen orientation only in mobile phones. Because I cannot provide the functionalities for mobile landscape mode as there is not an enough scape for it. So, how can I do it? The important thing is I don't want to lock the screen orientation for tabs/ iPads.

  • Chanaka Niroshan
    Chanaka Niroshan over 2 years
    Thanks a lot @Jignesh You solved my question. It's working fine.
  • Sachin
    Sachin over 2 years
    @ChanakaNiroshan - If above answer is correct then accept it. It will help other to find right solution.
  • Chanaka Niroshan
    Chanaka Niroshan over 2 years
    @Sachin - Yes. That answer is correct and I have thank him for the answer. Is there any other thing to do to accept correct answers?
  • Sachin
    Sachin over 2 years