所以我制作了一個調查表,資料/問題來自 api,如果未填寫問題答案但轉到下一頁的按鈕不可用,每個資料項都有“必需”鍵。如何if else為它創造條件?
表格

資料
{
"success": true,
"msg": "OK",
"data": [
{
"id": 4,
"label":
"Is this a test ride period or demonstration period in this PoC?",
"required": true,
"options": [
{"label": "Test ride", "value": "1"},
],
},
{
"id": 3,
"label": "Have you answered this survey before?",
"required": true,
"options": [
{"label": "No, for the first time", "value": "1"},
],
}
]
},
uj5u.com熱心網友回復:
一種方法是使用來自小部件的回呼作為選項。
class Data {
int id; // Will be unique
String label;
bool requireds;
List<Map<String, String>> options;
bool added; // Add an extra parameter in the class to manage if the value is selected or not.
Data({
required this.id,
required this.label,
required this.requireds,
required this.options,
this.added = false, // Set the default value as false
});
}
// Contains the list of data objects
List<Data> listData = [...];
// Callback from widgets to the parent widget
void updateData(bool isAdded, int id) {
listData.firstWhere((element) => element.id == id).added = true; // Update the added bool to true whenever a value is selected for the widget
}
// On click condition, can be added to state updater as well to make
// the button disabled or enabled
void checkConditions() {
List<Data> filter = [];
filter.addAll(listData);
// Filter the list for the condition
filter.retainWhere((data){
return data.requireds && !data.added; // If required and not added
});
// If the filter list is empty then all the required fields are answered
if (filter.isEmpty) {
// Go to next page or do an operation
} else {
// Do operation based on filter list values
}
}
這是我在 dart 中寫的代碼片段,還沒有在 Flutter 專案上嘗試過。讓我知道這是否有幫助或您面臨其他一些問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448250.html
下一篇:端點的正確REST樣式?
