I have an app using a GridView
to try and lay out some Card
s. The relevant code looks a bit like this
body: new GridView.count(
crossAxisCount: 2,
children: [new Card(
child: new Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const ListTile(
leading: const Text("0", style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 24.0)),
title: const Text('Some Heading'),
subtitle: const Text('My Subtitle'),
),
new ButtonTheme
.bar( // make buttons use the appropriate styles for cards
child: new ButtonBar(
children: <Widget>[
new FlatButton(
child: const Text('CALL TO ACTION'),
onPressed: () {
/* ... */
},
),
],
),
),
],
),
),
// same card repeated
],
),
Unfortunately, it doesn't render well:
The cards are too tall, they should end just below the "CALL TO ACTION" button. How can I fix this, and have the grid rows automatically respect the height of the contents?
child aspect ratio is basically width/height of the grid. So let's say you want the width of each grid to be 30 and the height to be 20, you would set the aspect ratio to be 3/2.
SliverGridDelegateWithFixedCrossAxisCount class Null safety. Creates grid layouts with a fixed number of tiles in the cross axis. For example, if the grid is vertical, this delegate will create a layout with a fixed number of columns.
You just need to put your GridView Widget into the Expanded Widget, for example: body: new Column( children: <Widget>[ new Expanded( child: GridView. count( // Create a grid with 2 columns. If you change the scrollDirection to // horizontal, this would produce 2 rows.
Your problem is that the tiles of GridView.count
have a default aspect ratio of 1.0
(i.e. square), and it sounds like you want your tiles to be shorter than that.
A quick fix would be to hard code the childAspectRatio
to a number you like. A more nuanced approach would be to implement a SliverGridDelegate
that describes the layout you want and use GridView.custom
. You could also just do a ListView
of 2-element Row
widgets and not use GridView
at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With