Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setPreferredOrientations based on device screen size [Flutter]

Tags:

flutter

I want to set orientation portrait only for small screen devices. At the moment I set everything to portrait orientation like so:

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(MyApp());
}

How can I set portrait orientation only if screen width is below a certain pixel count? (eg: 500px)

Thanks

like image 640
owlbear Avatar asked Oct 27 '25 10:10

owlbear


1 Answers

You can use MediaQueryData.fromWindow to get get the size. You can use it to get the scren width.

Future main() async {
  WidgetsFlutterBinding.ensureInitialized();
  final double screenWidth = MediaQueryData.fromWindow(WidgetsBinding.instance.window).size.width;
  if (screenWidth < 500) {
    await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  }

  runApp(MyApp());
}

like image 54
Vineet Avatar answered Oct 30 '25 01:10

Vineet