在我的一个 flutter 页面上,我需要将屏幕设置为横向模式并将其锁定,这样它就不能旋转到纵向模式,而只能在一页上。因此需要一种方法来即时启用此功能。有人知道怎么做吗?
我希望它旋转横向左或横向右,只是不进入纵向模式。
最佳答案
首先导入服务包:
import 'package:flutter/services.dart';
这将使您能够访问 SystemChrome
类,该类“控制操作系统图形界面的特定方面以及它与应用程序的交互方式。”
当您加载 Widget 时,请执行以下操作:
@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();
}
关于Flutter:如何按需设置和锁定屏幕方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50322054/