繁体   English   中英

根据Flutter中的内容适配一个Card高度

[英]Adapt a Card height according to content in Flutter

我有一个Card ,由标题、描述和底部的图标按钮栏组成。

只有描述具有可变大小。

我希望当我的卡片被创建时它会调整它的高度

这里是描述的小部件:

///Widget for the description of the post
  Widget _descriptionPost()
  {
    return Container(
      margin: const EdgeInsets.only(left: 7, top: 10),
      child: Text(
        widget.postModel.descriptionPost,
        maxLines: 4,
        overflow: TextOverflow.ellipsis,
        style: const TextStyle(fontSize: 16),
      ),
    );
  }

这是构建:

@override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width/1.2,
      height: HERE I WANT ADAPT THE HEIGHT,
      margin: const EdgeInsets.only(left: 5, right: 5, top: 5, bottom: 5),
      child: Card(
        elevation: 3,
        child: InkWell(
          onTap: ()
          {
            Navigator.pushNamed(context, '/post_detail', arguments: widget.postModel);
          },
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _profilePicAndUsernameAndTime(),
              _titlePostAndNameGame(),
              _descriptionPost(),
              Spacer(),
              _bottomBar()
            ],
          ),
        ),
      ),
    );
  }

您应该使用Containerconstraints属性并根据您的需要进行设置。

constraints: const BoxConstraints(minHeight: 0, maxHeight: 200.0)

这将确保您的Container将根据其child项具有0200.0之间的任何 H 值。

但是,请记住,如果child需要的高度超过您的maxHeight ,您将遇到溢出,因此您应该只在您有必要的最大高度时才使用它,否则将其留空。

暂无
暂无

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

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