[英]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()
],
),
),
),
);
}
您应该使用Container
的constraints
属性并根据您的需要进行设置。
constraints: const BoxConstraints(minHeight: 0, maxHeight: 200.0)
这将确保您的Container
将根据其child
项具有0
到200.0
之间的任何 H 值。
但是,请记住,如果child
需要的高度超过您的maxHeight
,您将遇到溢出,因此您应该只在您有必要的最大高度时才使用它,否则将其留空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.