我正在嘗試根據收到的資料動態擴展和折疊容器。目前我正在這樣做:
AnimatedContainer(
duration: const Duration(milliseconds: 550),
margin: EdgeInsets.symmetric(horizontal: 16),
constraints: !isExpanded
? BoxConstraints(
maxHeight: MyUtils.height(221),
minHeight: MyUtils.height(221))
: BoxConstraints(
minHeight: MyUtils.height(321),
maxHeight: MyUtils.height(1000)),
decoration: BoxDecoration(
color: containerColor,
borderRadius: const BorderRadius.all(Radius.circular(8))),
child: Column(children: [
Container(
margin: EdgeInsets.fromLTRB(8, 10, 8, 20),
height: MyUtils.height(36),
width: double.infinity,
decoration: BoxDecoration(
color: containerColor3,
borderRadius: BorderRadius.all(Radius.circular(6))),
),
InkWell(
onTap: () => expand(),
child: Text(
'Show',
style: TextStyle(color: Colors.white),
),
)
]),
),
函式 expand() 就像改變 isExpanded 的布林值一樣簡單。我設法折疊/展開容器,但問題是容器展開到 maxHeight,我想根據里面的專案使展開的高度動態。所以我嘗試只設定 minHeight 并希望 maxHeight 會跟隨專案,但我收到了一個錯誤,因為 maxHeight 沒有設定。
有什么辦法可以做到這一點?
uj5u.com熱心網友回復:
使用這個包flutter_staggered_grid_view
uj5u.com熱心網友回復:
使用Wrap()
Widget 它可以隨心所欲地解決您的問題 -
The function expand() is as simple as changing the bool of isExpanded. I managed to collapse/expand the Container, but the problem is that the Container expanded until maxHeight, I want to make the expanded height dynamic based on the items inside. So I tried to set only minHeight and hoping that maxHeight will follow the items, but I got an error because maxHeight is not set.
您可以使用Wrap
小部件管理高度
uj5u.com熱心網友回復:
默認情況下,Column 盡可能地嘗試。您可以通過指定mainAxisSize引數來控制此行為。
Column(
mainAxisSize: MainAxisSize.min, // ADD THIS LINE TO YOUR COLUMN
children: [...]
)
我建議你閱讀這篇文章https://docs.flutter.dev/development/ui/layout/constraints
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/526212.html
標籤:扑镖前端
上一篇:在腳本中實作多執行緒/并行處理
下一篇:如何測量飛鏢中一段代碼的持續時間