如何允许平板电脑横向,但在手机上阻止横向?

How to allow landscape for tablets, but prevent it on mobile phones?

我在手机上强制使用纵向模式,

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

        SystemChrome.setPreferredOrientations([
          DeviceOrientation.portraitUp,
          DeviceOrientation.portraitDown,
        ]);
  //...

我想在平板电脑(>=600)上启用横向,然后我尝试做类似的事情:

  final _size = MediaQuery.of(context).size;
  if(_size.width < 600) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
  }

...但它不起作用 (no media query widget ancestor error)

如何在平板电脑上允许竖屏和横屏,但在手机上强制竖屏?

你可以试试...

final data = MediaQueryData.fromWindow(WidgetsBinding.instance!.window);
if(data.size.shortestSide < 600) {
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
}

它对我有用。我希望它能帮助别人