Flutter 如何设置包裹 gridview 高度的列
Flutter how to set a column that wrap gridview height
我试图将列的高度设置为动态的,因为我拥有的产品越多,我需要的列的高度就越高,下面是我的代码
Container(
width: double.infinity,
height: 1000,
child: GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: productsList.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 22 / 30,
mainAxisSpacing: 5,
crossAxisSpacing: 3),
itemBuilder: (ctx, i) {
return ChangeNotifierProvider.value(
value: productsList[i],
child: FeedsProduct(),
);
},
),
),
如何动态设置容器高度?
GridView 中有一个 属性 叫做:shrinkWrap,见 shrinkWrap property
在这里你可以找到简单的例子
class SimpleExample extends StatelessWidget {
const SimpleExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: SingleChildScrollView(
child: Column(
children: [
GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: 30,
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 22 / 30,
mainAxisSpacing: 5,
crossAxisSpacing: 3),
itemBuilder: (ctx, i) {
return Container(
color: Colors.grey,
child: Center(
child: Text("Item $i"),
),
);
},
),
],
),
),
);
}
}
我不知道你的代码是如何工作的,但你可以将 Expanded() 小部件包装在 GridView() 而不是容器上:
Expanded(
child: GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: productsList.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 22 / 30,
mainAxisSpacing: 5,
crossAxisSpacing: 3),
itemBuilder: (ctx, i) {
return ChangeNotifierProvider.value(
value: productsList[i],
child: FeedsProduct(),
);
},
),
);
我试图将列的高度设置为动态的,因为我拥有的产品越多,我需要的列的高度就越高,下面是我的代码
Container(
width: double.infinity,
height: 1000,
child: GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: productsList.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 22 / 30,
mainAxisSpacing: 5,
crossAxisSpacing: 3),
itemBuilder: (ctx, i) {
return ChangeNotifierProvider.value(
value: productsList[i],
child: FeedsProduct(),
);
},
),
),
如何动态设置容器高度?
GridView 中有一个 属性 叫做:shrinkWrap,见 shrinkWrap property
在这里你可以找到简单的例子
class SimpleExample extends StatelessWidget {
const SimpleExample({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
child: SingleChildScrollView(
child: Column(
children: [
GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: 30,
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 22 / 30,
mainAxisSpacing: 5,
crossAxisSpacing: 3),
itemBuilder: (ctx, i) {
return Container(
color: Colors.grey,
child: Center(
child: Text("Item $i"),
),
);
},
),
],
),
),
);
}
}
我不知道你的代码是如何工作的,但你可以将 Expanded() 小部件包装在 GridView() 而不是容器上:
Expanded(
child: GridView.builder(
physics: NeverScrollableScrollPhysics(),
itemCount: productsList.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: 22 / 30,
mainAxisSpacing: 5,
crossAxisSpacing: 3),
itemBuilder: (ctx, i) {
return ChangeNotifierProvider.value(
value: productsList[i],
child: FeedsProduct(),
);
},
),
);