根据Flutter中的内容调整卡的高度



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

只有描述具有可变大小

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

这里是小部件的描述:

///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更高的高度,则会出现溢出,因此只有在需要最大高度时才应使用此选项,否则请将其留空。

最新更新