我正在嘗試在 Flutter 中實作自己的“可擴展面板”,基本上有問題和答案,自然都有不同的長度。我想要做的是首先在容器中顯示問題,當用戶單擊容器時,我想擴展第二個容器的高度,其中包含答案文本,從 0 到它的答案需要的高度值
double _theHeight = 0;
Column(
children: [
Container(
child:Inkwell(
onTap: () {setState({_theHeight = ????})},
child: Text(theQuestion)
)
),
Container(
height = _theHeight,
child: Text(theAnswer),
),
]
)
在這樣的例子中,我試圖給 _theHeight 變數一個常數值,比如 200,但由于不同的答案有不同的長度,這會導致溢位或太多不必要的地方。
是否有高度的默認值,以便父小部件僅覆寫其子小部件所需的空間?所以我可以輸入如下內容:
_theHeight = Container.default.height;
感謝您的時間。
uj5u.com熱心網友回復:
您可以簡單地將答案包裝在容器內。不需要給出具體的高度。您可以檢查小部件是否已擴展
例子:
bool isExpanded = false;
Column(
children: [
Container(
child:Inkwell(
onTap: () {setState({isExpanded = !isExpanded})},
child: Text(theQuestion)
)
),
if(isExpanded)
Container(
child: Text(theAnswer),
),
]
)
對于影片容器,您可以使用類似的 AnimatedSize
AnimatedSize(
duration: const Duration(seconds: 1),
child: isExpanded ? Container() : const Text(theAnswer),
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/491016.html
