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';
}
Author by
Chanaka Niroshan
Updated on January 01, 2023Comments
-
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.
-
Calaom over 2 yearsThis question should give you all you need: stackoverflow.com/questions/49484549/…
-
-
Chanaka Niroshan over 2 yearsThanks a lot @Jignesh You solved my question. It's working fine.
-
Sachin over 2 years@ChanakaNiroshan - If above answer is correct then accept it. It will help other to find right solution.
-
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 over 2 yearsCheck this - stackoverflow.com/help/someone-answers