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.
Asked
Active
Viewed 270 times
2
-
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 Answers
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
-
Thanks a lot @Jignesh You solved my question. It's working fine. – Chanaka N. Bandara Sep 30 '21 at 17:26
-
@ChanakaNiroshan - If above answer is correct then accept it. It will help other to find right solution. – Sachin Oct 12 '21 at 06:08
-
@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? – Chanaka N. Bandara Oct 12 '21 at 07:03
-
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