GridView containing Cards doesn't calculate he

2019-07-11 02:55发布

问题:

I have an app using a GridView to try and lay out some Cards. 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?

回答1:

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.



标签: dart flutter