小编典典

Flutter:如何按需设置和锁定屏幕方向

flutter

在我的第一页上,我需要将屏幕设置为横向模式并锁定它,以使其不能旋转到纵向模式,而只能在一页上旋转。因此需要一种即时启用此功能的方法。有人知道怎么做吗?

我希望它可以向左旋转风景或向右旋转风景,只是不进入纵向模式。


阅读 954

收藏
2020-08-13

共1个答案

小编典典

首先导入服务包:

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();
}
2020-08-13