我想在(學生)的串列視圖構建器中添加兩個成對的(存在和不存在)單選按鈕。 學生出勤串列視圖
到目前為止,我能夠創建一個串列視圖構建器,在每個串列行中使用成對的單選按鈕:
Expanded(
flex: 9,
child: ListView.builder(
itemCount: listStudents.length,
itemBuilder: (_, index) {
return Container(
margin: const EdgeInsets.all(8),
padding: const EdgeInsets.symmetric(horizontal: 15),
// alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.white70,
borderRadius: BorderRadius.circular(15)
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Wrap(
children: [
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SmallText(text: listStudents[index].name!, fontFamily: 'Poppins'),
],
),
],
),
Wrap(
children: [
Radio(
value: "absent$index",
groupValue: attendenceValue,
fillColor: MaterialStateColor.resolveWith((states) => Colors.redAccent),
onChanged: (val) {
setState(() {
attendenceValue = val.toString();
});
},
),
Radio(
value: "present$index",
groupValue: attendenceValue,
fillColor: MaterialStateColor.resolveWith((states) => Colors.greenAccent),
onChanged: (val) {
setState(() {
attendenceValue = val.toString();
});
},
),
],
),
],
),
);
}
),
)
目標是選擇必須在串列中每行的兩個成對的單選按鈕之間(意思是如果我標記第一個學生出現并轉到下一個學生,第一個選擇應該仍然存在)。但是,整個串列中的選擇會發生變化(之前的選擇不會持續存在)
請協助
uj5u.com熱心網友回復:
那是因為groupValue
所有串列項都使用相同的單選按鈕。您還應該將 a list
ofattendanceValue
與相同的listStudents
串列項一起使用。
將一對單選按鈕視為一對真或假按鈕。對于一個item,如果點擊了true,那么false是不能點擊的。所以,這兩個按鈕必須共享一些邏輯。來了groupValue
。如果單擊其中一個單選按鈕,它會搜索相同groupValue
的屬性并將單擊按鈕的屬性設定為 true,將其他按鈕的屬性設定為 false。
這是我修改了您的代碼的內容。玩得開心TT
https://dartpad.dev/?id=4e534fba753e188ca4c08ae0cbf6273e
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/517733.html
標籤:扑镖