如何仅针对Flutter中的某些类防止设备旋转?

时间:2020-01-28 21:38:14

标签: flutter dart orientation flutter-layout screen-orientation

我正在开发Flutter应用,我需要仅在某些类中阻止设备旋转,而在其他类中保持设备运行。我现在如何在应用程序中全局禁用旋转,但是如何仅在某些类中禁用旋转?

1 个答案:

答案 0 :(得分:2)

您可以尝试在窗口小部件中使用以下内容:

// to lock in landscape view
@override
void initState() {
  super.initState();
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeRight,
    DeviceOrientation.landscapeLeft,
  ]);
}

@override
dispose() {
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeRight,
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  super.dispose();
}

使用initState()dispose()的事实意味着,如果还没有使用StatefulWidget的话。