flutter学习部落

Flutter如何强制竖屏

2018-08-20  本文已影响2261人  吉原拉面

  如何在用户旋转屏幕的时候强制竖屏?

  解决方法:

void main(){
  ///
  /// 强制竖屏
  /// 
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown
  ]);

  runApp(new MyApp());
}

  在App启动的时候调用SystemChrome.setPreferredOrientations([…]) 方法。
如果想要强制横屏,则将SystemChrome.setPreferredOrientations([…]) 方法中的传参改掉即可:

  SystemChrome.setPreferredOrientations([
    DeviceOrientation.landscapeLeft,
    DeviceOrientation.landscapeRight
  ]);

补充:
  有人反应说这个方法不生效,可能是因为setPreferredOrientations方法返回的是一个Future,也就是执行runApp的时候,配置还未生效,可以试下以下方法:

void main() {
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
    .then((_) {
      runApp(new MyApp());
    });
}
上一篇下一篇

猜你喜欢

热点阅读