你怎么能幫我?如何使用 List <String'> 條件創建條件 If Else 陳述句?我已經嘗試了很多可能可以解決我的麻煩的事情。但我還是被卡住了。
所以,我想在 List<String'> status = 'Available' 它的顏色設定為綠色時創建一個條件,當 List<String'> status = 'Not Available' 它的顏色將設定為紅色。
這是我的代碼:
List<String> status = ['Available','Not Available','Available','Available','Not Available'];
_getMyColor(status){
if(status == "Available"){
return Color(0x00FF00);
} else {
return Color(0xFF0000);
}
}
然后我將此函式呼叫到我的小部件中(當然使用 ListView.builder)
Text(status[index]), style: TextStyle(color: _getMyColor(status))
該代碼沒有收到錯誤訊息,但它使我的 Text 小部件消失了。
我真的很感激任何答案
uj5u.com熱心網友回復:
我建議您修復 flutter 中的型別,以便您可以更好地更改代碼,如下所示:
List<String> status = ['Available','Not Available','Available','Available','Not Available'];
Color _getMyColor(String status){
if(status == "Available"){
return Color(0x00FF00);
} else {
return Color(0xFF0000);
}
}
你可以像這樣簡單地使用它:
Text(status[index]), style: TextStyle(color: _getMyColor(status[index]))
uj5u.com熱心網友回復:
要獲得更好的結構,請使用列舉:
enum MyStatus{
available, notAvailable
}
然后
Color _getMyColor(MyStatus status){
switch(status){
case MyStatus.available:
return const Color(0x0000ff00);
case MyStatus.notAvailable:
return const Color(0x00ff0000);
}
}
uj5u.com熱心網友回復:
就像 Benyamin Beyzaie 在他的回答中顯示的那樣,您在呼叫 _getMyColor 時錯過了串列索引器
...(color: _getMyColor(status[index]))
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/362911.html
