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
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());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With