我想禁用横向模式。我尝试使用以下代码仅允许纵向模式。但这不适用于我的物理设备。该如何解决?
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
runApp(MyApp());
}
答案 0 :(得分:2)
您需要将代码粘贴到Widget build()
中。考虑此answer以获得更多详细信息
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
return new MaterialApp(...);
}
}