简体   繁体   English

为 Flutter 的 Card 小部件设置特定高度

[英]Setting a specific height to Flutter's Card widget

I'm trying to expand the height of a given grid of cards, so they are able to fit some more information than they currently do.我正在尝试扩展给定卡片网格的高度,以便它们能够容纳比目前更多的信息。 These cards are wrapped by a GridView.count() that is shrinked, since I'm going to put more things below this widget.这些卡被一个缩小的GridView.count()包裹,因为我要在这个小部件下面放更多东西。

As for now, the cards look like these, in which you can see that one of them overflows the text at the bottom, which is an undesired behavior (especially when I want the cards to have some bottom padding):至于现在,卡片看起来像这样,您可以看到其中一个溢出了底部的文本,这是一种不受欢迎的行为(尤其是当我希望卡片有一些底部填充时):

在此处输入图像描述

Being this the case, I would like to know if it's possible to manually change the card's height.在这种情况下,我想知道是否可以手动更改卡的高度。 I'm maybe letting this concrete configuration stay and remove some info, since I like the fact that the cards currently maintain their 1x1 proportion, but for curiosity sake, I would like to discover how to do this.我可能会保留此具体配置并删除一些信息,因为我喜欢卡片当前保持其 1x1 比例的事实,但出于好奇,我想知道如何做到这一点。

I tried many things, such as wrapping the Card widget with a Container or a SizedBox and manually setting the height, but none of these approaches change anything.我尝试了很多东西,例如用ContainerSizedBox包装Card小部件并手动设置高度,但这些方法都没有改变任何东西。

I guess that the problem may be in the GridView itself.我想问题可能出在GridView本身。 This is how it looks:这是它的外观:

return FutureBuilder<List<Event>>(
        future: new EventsService().getEventsForCoords(),
        builder: (context, AsyncSnapshot<List<Event>> snapshot) {
          if (snapshot.hasData) {
            return GridView.count(
                crossAxisCount: 2,
                shrinkWrap: true,
                children: generateProximityEventCards(snapshot.data));

          } else {
            return CircularProgressIndicator();
          }
        });

As you can guess, the generateProximityEventCards method is the one that prints the Card widgets at the end.您可以猜到, generateProximityEventCards方法是最后打印Card小部件的方法。 This is how the method looks as for now:这是该方法现在的样子:

List<Widget> generateProximityEventCards(List<Event> eventList) {
    // Render each card
    return eventList.map((Event ev) {
      return Card(
          semanticContainer: true,
          clipBehavior: Clip.antiAliasWithSaveLayer,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10.0),
          ),
          elevation: 5,
          margin: EdgeInsets.all(7),
          child: SizedBox(
            height: 600,
            child: Column(
              children: <Widget>[
                Image(
                  alignment: Alignment.topCenter,
                  fit: BoxFit.fitWidth,
                  image: ev.imageUrl,
                  height: 110,
                  width: 200,
                ),
                Container(
                  child: Text(ev.name),
                  padding: EdgeInsets.only(left: 10, right: 10),
                ),
                Container(
                  child: Text(ev.startDate.toString()),
                  padding: EdgeInsets.only(left: 10, right: 10),
                ),
                Container(
                  child: Text(ev.address),
                  padding: EdgeInsets.only(left: 10, right: 10),
                ),
              ],
            ),
          ));
    }).toList();
  }

So, in conclussion: how can I change the height of the cards so they can hold more information?所以,结论是:我怎样才能改变卡片的高度,以便它们可以保存更多信息?

Thanks!谢谢!

GridView isn't really designed to have tiles of different size. GridView 并不是真正设计为具有不同尺寸的瓷砖。 A good option is to use the package flutter_staggered_grid_view .一个不错的选择是使用 package flutter_staggered_grid_view

动态图块大小

Now your tile sizes can even be dynamic, check out the code for the gif above!现在您的图块大小甚至可以是动态的, 请查看上面 gif 的代码!

To automatically fit some variable length text somewhere you can use the package auto_size_text .要在某处自动放置一些可变长度的文本,您可以使用 package auto_size_text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM