Flutter 如何在GridView.builder中设置容器的固定高度?不应依赖于childAspectRatio
实际上,我正试图在GridView.builder中显示一个产品列表,但它的高度会根据childAspectRatio根据不同高度的不同设备而变化。我想为每个设备修复它们 要执行类似操作,您可能需要使用 您将能够更轻松地调整布局,否则在GridView中,您别无选择,只能使用Flutter 如何在GridView.builder中设置容器的固定高度?不应依赖于childAspectRatio,flutter,flutter-layout,Flutter,Flutter Layout,实际上,我正试图在GridView.builder中显示一个产品列表,但它的高度会根据childAspectRatio根据不同高度的不同设备而变化。我想为每个设备修复它们 要执行类似操作,您可能需要使用 您将能够更轻松地调整布局,否则在GridView中,您别无选择,只能使用childAspectRatio 您可以将它与MediaQuery属性一起使用,以便在每个设备上实现良好的设计。我也遇到了同样的问题,并最终计算了childAspectRatio,这样每个单元格最终都具有我想要的高度 dou
childAspectRatio
您可以将它与
MediaQuery
属性一起使用,以便在每个设备上实现良好的设计。我也遇到了同样的问题,并最终计算了childAspectRatio
,这样每个单元格最终都具有我想要的高度
double cellWidth = ((MediaQuery.of(context).size.width - allHorizontalPadding) / columnCount);
double desiredCellHeight = 200;
double childAspectRatio = cellWidth / desiredCellHeight;
创造这样的东西 首先,创建一个数组类列表并重写构建方法
class Products extends StatefulWidget {
@override
_ProductsState createState() => _ProductsState();
}
class _ProductsState extends State<Products> {
var productList = [
{
'name': "Blazzer",
"picture": "images/products/blazer1.jpeg",
"old_price": 120,
"price": 85,
},
/*{
'name': "Blazzer",
"picture": "images/products/blazer2.jpeg",
"old_price": 120,
"price": 85,
},*/
{
'name': "Red Dress",
"picture": "images/products/dress1.jpeg",
"old_price": 120,
"price": 85,
},
/* {
'name': "Blazzer",
"picture": "images/products/dress2.jpeg",
"old_price": 120,
"price": 85,
}*/
];
@override
Widget build(BuildContext context) {
return GridView.builder(
itemCount: productList.length,
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
itemBuilder: (BuildContext context, int index) {
return SingleProduct(
productName: productList[index]['name'],
productPicture: productList[index]['picture'],
productOldPrice: productList[index]['old_price'],
productPrice: productList[index]['price'],
);
});
}
}
我也尝试过使用该软件包和MediaQuery,但没有得到预期的结果,无法在gridView中固定容器大小……好的,但现在还没有真正的方法做到这一点,您可以看看@Ovidiu解决方案,它使用
MediaQuery
,这是最合适的。allHorizontalPadding意味着填充:EdgeInsets对称(水平:7.0,垂直:10.0)7在这种情况下,对吗?|但仍然没有在所有设备中固定gridView中的卡高度。。计算allHorizontalPadding
的正确值很重要。例如,在上面的图像中,这将是左填充(7)+右填充(7)+网格单元之间的填充(?)横轴间距:10.0,填充:边设置。对称(水平:7.0,垂直:10.0),因此我在allHorizontalPadding中添加了24,但仍然无法确定GridView中的卡的高度。您确定没有遗漏什么吗?上面的图像是否准确?您的横轴间距在视觉上比上面的图像中的水平填充小。生成的高度是否始终小于或始终大于所需的?(使用颤振检查器查找准确的高度)。您是否有基于屏幕大小的动态填充或网格属性?如果有问题,请在此处签出代码。让我知道
class SingleProduct extends StatelessWidget {
final productName;
final productPicture;
final productOldPrice;
final productPrice;
const SingleProduct(
{this.productName,
this.productPicture,
this.productOldPrice,
this.productPrice});
@override
Widget build(BuildContext context) {
return Card(
child: Hero(
tag: productName,
child: Material(
child: InkWell(
onTap: () => Navigator.of(context).push(MaterialPageRoute(
builder: (context) => ProductDetails(
//Passing Product details to another activity
productDetailName: productName,
productDetailNewPrice: productPrice,
productDetailOldPrice: productOldPrice,
productDetailPicture: productPicture,
))),
child: GridTile(
footer: Container(
color: Colors.white70,
child: ListTile(
leading: Text(
productName,
style: TextStyle(fontWeight: FontWeight.bold),
),
title: Text(
"\$$productPrice",
style: TextStyle(
color: Colors.red, fontWeight: FontWeight.w800),
),
subtitle: Text(
"\$$productOldPrice",
style: TextStyle(
color: Colors.black54,
fontWeight: FontWeight.w800,
decoration: TextDecoration.lineThrough),
),
),
),
child: Image.asset(
productPicture,
fit: BoxFit.cover,
)),
),
),
),
);
}
}