如何检测 Flutter 中布局的方向变化? [英] How to detect orientation change in layout in Flutter?

查看:35
本文介绍了如何检测 Flutter 中布局的方向变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Flutter 中确定 Orientation 是纵向还是横向

How to find out Orientation is portrait or landscape in Flutter

if(portrait){
  return ListView.builder()
}else{
  return GridView.count()
}

推荐答案

为了确定屏幕的 Orientation,我们可以使用 OrientationBuilder Widget.OrientationBuilder 将确定当前的 Orientation 并在 Orientation 改变时重建.

In order to determine the Orientation of the screen, we can use the OrientationBuilder Widget. The OrientationBuilder will determine the current Orientation and rebuild when the Orientation changes.

new OrientationBuilder(
  builder: (context, orientation) {
    return new GridView.count(
      // Create a grid with 2 columns in portrait mode, or 3 columns in
      // landscape mode.
      crossAxisCount: orientation == Orientation.portrait ? 2 : 3,
    );
  },
);

你可以在这里找到完整的例子:https://flutter.io/cookbook/design/orientation/

you can find the complete example here: https://flutter.io/cookbook/design/orientation/

这篇关于如何检测 Flutter 中布局的方向变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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