如何在 Flutter 中仅针对某些 类 防止设备旋转?
How to prevent device rotation only for certain classes in Flutter?
我正在开发一个 Flutter 应用程序,我只需要在某些 类 中防止设备旋转,同时在其他方面保持它正常工作。我现在如何在应用程序中全局禁用旋转,但如何仅在某些 类?
中禁用它
您可以尝试在您的小部件中使用类似这样的东西:
// 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
如果不是这样的话。
我正在开发一个 Flutter 应用程序,我只需要在某些 类 中防止设备旋转,同时在其他方面保持它正常工作。我现在如何在应用程序中全局禁用旋转,但如何仅在某些 类?
中禁用它您可以尝试在您的小部件中使用类似这样的东西:
// 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
如果不是这样的话。