如何检测Flutter中布局的方向变化? [英] How to detect orientation change in layout in Flutter?
本文介绍了如何检测Flutter中布局的方向变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在Flutter中确定方向是纵向还是横向
How to find out Orientation is portrait or landscape in Flutter
if(portrait){
return ListView.builder()
}else{
return GridView.count()
}
推荐答案
为了确定屏幕的方向,我们可以使用OrientationBuilder
小部件. 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屋!
查看全文