【问题标题】:How to prevent device rotation only for certain classes in Flutter? [duplicate]如何仅针对 Flutter 中的某些类防止设备旋转? [复制]
【发布时间】:2020-05-14 09:06:59
【问题描述】:

我正在开发一个 Flutter 应用程序,我只需要在某些类中防止设备旋转,同时在其他类中保持它的工作。我现在如何在应用程序中全局禁用旋转,但如何仅在某些类中禁用它?

【问题讨论】:

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


【解决方案1】:

你可以尝试在你的小部件中使用这样的东西:

// 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,则必须使用它。

【讨论】:

  • Guillaume Roux - 此限制也适用于 child screens / Navigator.push screens,这对我来说不是一个好的解决方案。任何其他建议将不胜感激。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多