[英]Dart: using variables as method placeholders
使用這個 function:
buildButton(int file_number, String yourcolor, [String text]){
return Expanded(
child: FlatButton(
color: Colors.yourcolor,
child: Text(text),
onPressed: (){
playaudio(file_number);
},
),);
}
color: Colors.yourcolor
。 我不斷收到錯誤:
The getter 'yourcolor' isn't defined for the class 'Colors'. Try importing the library that defines 'yourcolor', correcting the name to the name of an existing getter, or defining a getter or field named 'yourcolor'.
我知道沒有名為“yourcolor”的方法,但有沒有一種方法可以使用 function 參數獲得我想要的顏色?
您不能使用 String 來傳遞顏色。 你必須傳遞顏色本身。
buildButton(int file_number, Color yourcolor, [String text]){
return Expanded(
child: FlatButton(
color: yourcolor,
child: Text(text),
onPressed: (){
playaudio(file_number);
},
),);
}
例如,當您調用 function 時,將顏色傳遞為Colors.blue
。
感謝jamesdlin ,我能夠創建一個 map 將“yourcolor”指向它的相應顏色
buildButton(int file_number, String yourcolor){
Map match_colors = {'red':Colors.red, 'orange':Colors.orange, 'yellow':Colors.yellow,
'lightgreen': Colors.lightGreen, 'green':Colors.green, 'lightblue':Colors.lightBlue, 'purple':Colors.purple};
return Expanded(
child: FlatButton(
color: match_colors[yourcolor],
onPressed: (){
playaudio(file_number);
},
),
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.