我正在开发Flutter应用,我需要仅在某些类中阻止设备旋转,而在其他类中保持设备运行。我现在如何在应用程序中全局禁用旋转,但是如何仅在某些类中禁用旋转?
答案 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
的话。