[英]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.