【问题标题】:How to force the application to Portrait mode in flutter如何在颤动中强制应用程序进入肖像模式
【发布时间】:2019-01-24 07:03:39
【问题描述】:
void main(){
  ///
  /// Force the layout to Portrait mode
  /// 
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);

  runApp(new MyApp());
}

【问题讨论】:

标签: flutter


【解决方案1】:

将此代码放在 MyApp() 中,不要忘记导入服务包:

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);

如下:

import 'package:flutter/services.dart';

class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);
      return new MaterialApp();
    }
  }

更新

根据@F-1 的评论,有一个错误会阻止此代码在 ipad github.com/flutter/flutter/issues/27235 上运行。

下面的评论可能会对你有所帮助。

https://github.com/flutter/flutter/issues/27235#issuecomment-508995063

【讨论】:

  • 我刚刚编辑了这个,但它似乎无论如何都不起作用……至少在 iOS 中不起作用。
  • @Hasen 我已经添加了运行良好的代码。
  • 有一个错误阻止此代码在 ipad 上运行 github.com/flutter/flutter/issues/27235
【解决方案2】:

这是适用于AndroidiOS 的代码

Future main() async {
  await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  runApp(new MyApp());
}

【讨论】:

【解决方案3】:

以下代码对我有用。

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/splashScreen.dart';

    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      SystemChrome.setPreferredOrientations([
        DeviceOrientation.portraitUp,
        DeviceOrientation.portraitDown,
      ]);

  runApp(MaterialApp(home: splashScreen(),));
}

【讨论】:

    猜你喜欢
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    • 2020-09-26
    相关资源
    最近更新 更多