在我的一个Flutter 页面上,我需要将屏幕设置为横向模式并锁定,这样它就不能旋转到纵向模式,而只能在一个页面上.因此,我们需要一种方法来动态启用此功能.有人知道怎么做吗?
我想它旋转景观左或景观右,只是不进入肖像模式.
在我的一个Flutter 页面上,我需要将屏幕设置为横向模式并锁定,这样它就不能旋转到纵向模式,而只能在一个页面上.因此,我们需要一种方法来动态启用此功能.有人知道怎么做吗?
我想它旋转景观左或景观右,只是不进入肖像模式.
首先导入服务包:
import 'package:flutter/services.dart';
个
这将使您可以访问SystemChrome
类,其中"Controls specific aspects of the operating system's graphical interface and how it interacts with the application."
类
加载小部件时,请执行以下操作:
@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();
}