如何将GridView限制为屏幕的大小并将内部元素调整到不同设备上?
我有一个GridView,可以从3-12个元素转移,我希望所有这些元素都可以在屏幕上放入而不滚动。
我在Pixel 2个设备和一个尺寸为6.7的设备上进行了测试。使用childaspectratio
我选择了适合我这两个设备child aspectratio的比率:(1/0.8),
。当元素不占据整个屏幕时,还可以禁用滚动以进行中心。
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
但是这种方法对我来说似乎不是很漂亮,在像素2中有12个元素,它们不再适合。您能告诉我如何根据需要并仅占据屏幕空间而使GridView内部的传输元素收缩?
Expanded(
child: Container(
// height: size * 2,
// padding: EdgeInsets.all(5),
color: Colors.red,
child: Center(
child: GridView(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 5,
crossAxisSpacing: 5,
childAspectRatio: (1 / 0.8),
),
children: [
Padding(
padding: const EdgeInsets.all(5),
child: Container( color: Colors.grey,
child:
Image.network('https://picsum.photos/250?image=1'), padding: EdgeInsets.all(3),
),
),
Padding(
padding: const EdgeInsets.all(5),
child: Container( color: Colors.grey,
child:
Image.network('https://picsum.photos/250?image=1'), padding: EdgeInsets.all(3),
),
),
Padding(
padding: const EdgeInsets.all(5),
child: Container( color: Colors.grey,
child:
Image.network('https://picsum.photos/250?image=1'), padding: EdgeInsets.all(3),
),
),
],
),
),
),
flex:10
),
GridView is located in the red area
I have a GridView in which I will transfer from 3-12 elements, I would like all these elements to fit on the screen without scrolling.
I tested on Pixel 2 devices and a device with a size of 6.7. Using childAspectRatio
I selected the ratio that suits me for these two devices childAspectRatio: (1 / 0.8),
. And also disabled the scroll for centering when the elements do not occupy the entire screen.
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
But this method does not seem very beautiful to me, and with 12 elements in Pixel 2, they no longer fit. Could you tell me how to make the transmitted elements shrink inside the GridView depending on the need and occupy only the screen space ?
Expanded(
child: Container(
// height: size * 2,
// padding: EdgeInsets.all(5),
color: Colors.red,
child: Center(
child: GridView(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
mainAxisSpacing: 5,
crossAxisSpacing: 5,
childAspectRatio: (1 / 0.8),
),
children: [
Padding(
padding: const EdgeInsets.all(5),
child: Container( color: Colors.grey,
child:
Image.network('https://picsum.photos/250?image=1'), padding: EdgeInsets.all(3),
),
),
Padding(
padding: const EdgeInsets.all(5),
child: Container( color: Colors.grey,
child:
Image.network('https://picsum.photos/250?image=1'), padding: EdgeInsets.all(3),
),
),
Padding(
padding: const EdgeInsets.all(5),
child: Container( color: Colors.grey,
child:
Image.network('https://picsum.photos/250?image=1'), padding: EdgeInsets.all(3),
),
),
],
),
),
),
flex:10
),
GridView is located in the red area
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用:
Trying using: https://pub.dev/packages/flutter_staggered_grid_view
This package gives you multiple option and also helps you to make GridView Responsive