Flutter forces horizontal and vertical screens-SystemChrome

SystemChrome controls certain aspects of the operating system 's graphical interface and how it interacts with applications. It should be noted that when using it, it must be executed first in the entry main function WidgetsFlutterBinding.ensureInitialized();


void main() {
  // debugProfileBuildsEnabled = true;
  // debugProfilePaintsEnabled = true;
  // debugPaintLayerBordersEnabled = true;
  WidgetsFlutterBinding.ensureInitialized(); //不加这个强制横/竖屏会报错
  SystemChrome.setPreferredOrientations([
    // 强制竖屏
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);
  runApp(MaterialApp(home: AMapDemo()));
}

setPreferredOrientations Set horizontal screen or fixed screen.
Generally, we want to force the fixed screen. We only need to specify DeviceOrientation.portraitUp or DeviceOrientation.portraitDown. This way, even if the orientation lock is not turned on in the phone settings, you don’t have to worry about it being reversed.

To control the horizontal and vertical screens on a specific page, you can write:

  if (_landscapeScreen == true) {
      // 强制横屏
      SystemChrome.setPreferredOrientations(
          [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
    } else {
      SystemChrome.setPreferredOrientations(
          [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
    }

Guess you like

Origin blog.csdn.net/RreamigOfGirls/article/details/130972466