flutter how to give height to the childrens of GridView.Builder
I am tring to give height to the childrens of gridview.builder
but it's not accepting. I tried by using container but its not working...
please help
GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
),
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int index) {
return Container(
height: 280.0,
child: Column(
children: <Widget>[
Padding(
padding:
EdgeInsets.fromLTRB(10.0, 10.0, 10.0, 10.0),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 208.5,
width: 138.75,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0),
image: DecorationImage(
image: NetworkImage(
"${snapshot.data[index].url}"),
fit: BoxFit.fill)),
),
),
Text(
snapshot.data[index].title,
style: TextStyle(fontSize: 17.0),
),
],
),
);
},
),`
You want to use the childAspectRatio
property of the SliverGridDelegate
preferably with MediaQuery
:
class HomePage extends StatelessWidget {
final List<String> items = <String>[
"Item 1",
"Item 2",
"Item 3",
"Item 4",
"Item 5",
"Item 6",
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Home')),
body: GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
childAspectRatio: MediaQuery.of(context).size.width /
(MediaQuery.of(context).size.height / 4),
),
itemCount: items.length,
itemBuilder: (context, index) {
return GridTile(child: Text(items[index]));
},
),
);
}
}
You can use mainAxisExtent instead of childAspectRatio
GridView.builder(
physics: BouncingScrollPhysics(),
itemCount: resumes.length,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisExtent: 256,
),
itemBuilder: (_, index) => Container(),
),