在我的 Flutter 页面中,我需要将屏幕设置为横屏模式并锁定,使其无法旋转成竖屏模式,但仅限于该页面。因此需要一种方法来实时启用此功能。有人知道如何做到这一点吗?
我希望它能够旋转成左横屏或右横屏,但不要旋转成竖屏模式。
我希望它能够旋转成左横屏或右横屏,但不要旋转成竖屏模式。
首先导入服务包:
import 'package:flutter/services.dart';
这将使您可以访问SystemChrome类,它"控制操作系统图形界面的特定方面以及它如何与应用程序交互。"
当您加载小部件时,请执行以下操作:
@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();
}
我会使用一个简单的mixin来锁定手机为竖屏模式。以下解决方案可以将整个应用程序锁定为竖屏模式,或者将特定的屏幕设置为竖屏模式,同时保持其他地方的旋转。
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
/// Forces portrait-only mode application-wide
/// Use this Mixin on the main app widget i.e. app.dart
/// Flutter's 'App' has to extend Stateless widget.
///
/// Call `super.build(context)` in the main build() method
/// to enable portrait only mode
mixin PortraitModeMixin on StatelessWidget {
@override
Widget build(BuildContext context) {
_portraitModeOnly();
return null;
}
}
/// Forces portrait-only mode on a specific screen
/// Use this Mixin in the specific screen you want to
/// block to portrait only mode.
///
/// Call `super.build(context)` in the State's build() method
/// and `super.dispose();` in the State's dispose() method
mixin PortraitStatefulModeMixin<T extends StatefulWidget> on State<T> {
@override
Widget build(BuildContext context) {
_portraitModeOnly();
return null;
}
@override
void dispose() {
_enableRotation();
super.dispose();
}
}
/// blocks rotation; sets orientation to: portrait
void _portraitModeOnly() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
}
void _enableRotation() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight,
]);
}
App
小部件中实现PortraitModeMixin
。请记住在Widget build(BuildContext context)
方法中调用super.build(context)
。/// Main App widget
class App extends StatelessWidget with PortraitModeMixin {
const App();
@override
Widget build(BuildContext context) {
super.build(context);
return CupertinoApp(
title: 'Flutter Demo',
theme: CupertinoThemeData(),
home: Text("Block screen rotation example"),
);
}
}
/// Specific screen
class SampleScreen extends StatefulWidget {
SampleScreen() : super();
@override
State<StatefulWidget> createState() => _SampleScreenState();
}
class _SampleScreenState extends State<SampleScreen>
with PortraitStatefulModeMixin<SampleScreen> {
@override
Widget build(BuildContext context) {
super.build(context);
return Text("Flutter - Block screen rotation example");
}
@override
void dispose() {
super.dispose();
}
}
super.dispose()
。以下是错误信息:
I/flutter (29686): 在构建小部件树的最终阶段,发生了以下断言错误:
I/flutter (29686): _MultiPlayerAcceptPageState.dispose failed to call super.dispose.
I/flutter (29686): dispose()实现必须始终调用它们的超类dispose()方法,以确保小部件使用的所有资源都被完全释放。 - Angel Todorovdispose()
被注释为@mustCallSuper
。这意味着你需要在mixin函数本身中添加super.dispose()
。 - Chirag Arorareturn null
替换为return super.build(context);
,它就可以正常工作了。 - its_broke_again首先,将整个应用程序的方向锁定为纵向模式。
//Do this in main.dart
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
.then((_) {
runApp(MyApp());
});
其次,进入您想要更改方向的特定屏幕。
@override
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft
]);
}
@override
void dispose() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
super.dispose();
}
要使用SystemChrome,您需要添加'package:flutter/services.dart'
。
void main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
.then((_) {
runApp(new MyApp());
});
}
import 'package:flutter/services.dart';
main() {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
runApp(MyHomePage());
}
WidgetsFlutterBinding.ensureInitialized();
这个方法让我的解决方案起作用了。很好的发现! - AWPimport services.dart
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp]
)
.then((_) {
runApp(new MyApp());
});
}
// 等待应用程序初始化后设置屏幕方向,然后锁定方向
iOS重要提示
步骤
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp,DeviceOrientation.portraitDown,]);
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.landscapeRight,
DeviceOrientation.landscapeLeft,
]);
}
@override
dispose(){
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
super.dispose();
}
简单的方法来锁定整个应用程序的屏幕方向
在main.dart
文件开头添加import 'package:flutter/services.dart';
。
在MyApp
类的Widget构建区域中,在return
部分之前创建SystemChrome.setPreferredOrientations();
方法来禁止屏幕旋转。
使用[DeviceOrientation.<orientation-type>]
将方向指定为方法参数。
在<orientation-type>
位置上使用以下之一:
portraitUp
portraitDown
landscapeLeft
landscapeRight
示例代码:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' ;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
]);
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Screen Orientation"),
),
body: Container(
),
),
);
}
}
导入services.dart
,您的void main
函数应该是这样的:
void main(){
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
.then((_){
runApp(MyApp());
}
);
}
在主文件夹中的AndroidManifest.xml文件中,找到activity标签并设置android:screenOrientation="portrait"。
<activity android:windowSoftInputMode="adjustResize" android:screenOrientation = "portrait">