Flutter:如何按需设置和锁定屏幕方向 [英] Flutter: How to set and lock screen orientation on-demand

查看:480
本文介绍了Flutter:如何按需设置和锁定屏幕方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

On one of my flutter pages, I need the screen to set to landscape mode and lock it so it can't rotate into portrait mode, but only on the one page. So need a way to enable this function on-the-fly. Anyone know how to do this?

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

I would like it to rotate landscape-left or landscape-right, just not into portrait mode.

推荐答案

首先导入服务包:

import 'package:flutter/services.dart';

这将使您可以访问SystemChrome类,该类为"Controls specific aspects of the operating system's graphical interface and how it interacts with the application."

This will give you access to the SystemChrome class, which "Controls specific aspects of the operating system's graphical interface and how it interacts with the application."

加载小部件时,请执行以下操作:

When you load the Widget, do something like this:

@override
void initState(){
  super.initState();
  SystemChrome.setPreferredOrientations([
      DeviceOrientation.landscapeRight,
      DeviceOrientation.landscapeLeft,
  ]);
}

然后,当我离开页面时,将其恢复为正常状态:

then when I leave the page, put it back to normal like this:

@override
dispose(){
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeRight,
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  super.dispose();
}

这篇关于Flutter:如何按需设置和锁定屏幕方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆