微信交流群

老孟公众号

# OrientationBuilder

当手机的屏幕由横屏切换竖屏时,UI布局通常也会发生变化,可以通过OrientationBuilder来实现此效果,用法如下:

OrientationBuilder(
  builder: (BuildContext context, Orientation orientation) {
    int count = orientation == Orientation.portrait ? 3 : 5;
    return GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: count, crossAxisSpacing: 2, mainAxisSpacing: 4),
      itemBuilder: (context, index) {
        return Container(
          color: Colors.primaries[index % Colors.primaries.length],
        );
      },
      itemCount: 30,
    );
  },
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

竖屏效果:

image-20200421204941325

横屏效果:

image-20200421204915330