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


Solution 1: Vineet

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());
}