颤动旋转屏幕

问题描述 投票:0回答:1
void _rotateScreen() {
    final currentOrientation = MediaQuery.of(context).orientation;

    if (currentOrientation == Orientation.portrait) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitDown,
      ]);
    } else {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
      ]);
    }
    setState(() {});
  }

当我单击按钮时,屏幕向下旋转,但如果屏幕向下旋转,则不会再次纵向向上

android flutter rotation screen
1个回答
0
投票

您必须在 main.dart 中定义默认屏幕方向,如下所示:

Future main() async {
   WidgetsBinding widgetsBinding = 
         WidgetsFlutterBinding.ensureInitialized();
 SystemChrome.setPreferredOrientations(
  [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);

当您需要在特定屏幕(StatefulWidget)上使用其他方向时,请在 init 方法中使用它:

  @override
  void initState() {
     super.initState();
     SystemChrome.setPreferredOrientations([
       DeviceOrientation.landscapeRight,
       DeviceOrientation.landscapeLeft,
    ]);
  _initGame();
 }

在 on dispose 方法中,您可以将方向设置回来,如下所示:

  @override
  dispose() {
  SystemChrome.setPreferredOrientations([
     DeviceOrientation.portraitUp,
     DeviceOrientation.portraitDown,
  ]);
  super.dispose();
  }
© www.soinside.com 2019 - 2024. All rights reserved.