2

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 N. Bandara
  • 247
  • 1
  • 3
  • 10
  • This question should give you all you need: https://stackoverflow.com/questions/49484549/can-we-check-the-device-to-be-smartphone-or-tablet-in-flutter – Calaom Sep 29 '21 at 14:37

2 Answers2

1
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';
         }
Jignesh Patel
  • 223
  • 1
  • 4
0
void main() async {

  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

  runApp(MyApp());
}

use it in main.dart like this. Change DeviceOrientation as you want. Add more DeviceOrientation or remove. Whatever you want

Aslan Demir
  • 54
  • 1
  • 5