我在 CheckboxListTile 小部件中有一個復選框串列,然后我映射串列并將真實值添加到名為 _saved 的集合中,如下所示
ListView(
shrinkWrap: true,
physics: ScrollPhysics(),
children: interestList.entries.map<Widget>((entry) {
return new CheckboxListTile(
title: new Text(entry.key),
value: entry.value,
activeColor: Colors.black,
checkColor: Colors.white,
onChanged: (bool? value) {
setState(() {
interestList[entry.key] = value!;
if (value == true) {
_saved.add(entry.key);
} else {
_saved.contains(entry.key)
? _saved.remove(entry.key)
: debugPrint("ok");
}
debugPrint(_saved.toString());
});
},
);
}).toList(),
),
然后我通過映射到 _saved 來渲染包裝小部件中的真實專案,如下所示。所以我想向渲染的專案添加一個 onTap,這樣當我點擊這個專案時,它會從 CkeckboxList 中取消選中它
Wrap(
children: _saved
.map<Widget>(
(item) => FittedBox(
fit: BoxFit.fill,
child: Container(
margin: const EdgeInsets.all(8.0),
padding: const EdgeInsets.all(5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(6.0)),
border: Border.all(color: Colors.grey)),
child: Center(
child: GestureDetector(
onTap: () {
// _removeInterest(item);
},
child: Text(item " " "x")))),
),
)
.toList());
}
以下是興趣清單
Map<String, bool> interestList= {
'Bubble Football ?': false,
'Futsal ??': false,
'Beach Volleyball ??': false,
'Volleyball ??': false,
'Dodgeball ??': false,
'Rugby ??': false,
'American Footbal ??': false,
'Korftbal ??': false,
'Netbal ?': false,
};
最終設定 _saved = Set();
我試圖將波紋管函式傳遞給 onTap,
_removeInterest(entry) {
_saved.contains(entry) ? _saved.remove(entry) : debugPrint("");
setState(() {});
}
謝謝你們
uj5u.com熱心網友回復:
您正在洗掉它,_saved但復選框串列會遍歷interestList,因此您也應該進行修改interestList。
所以你_removeInterest應該是這樣的
_removeInterest(entry) {
_saved.contains(entry) ? _saved.remove(entry) : debugPrint("");
interestList[entry] = false;
setState(() {});
}
編輯

uj5u.com熱心網友回復:
您需要使用的鍵interestList來獲取和設定value的復選框,另一個串列添加或洗掉上面selected option's一行。試試下面的代碼:
Column(
children: [
SizedBox(
height: 40,
width: MediaQuery.of(context).size.width,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: _list.length,
itemBuilder: (context, index) => Row(
children: [
Text("${_list[index]}"),
IconButton(
icon: Icon(Icons.clear_rounded),
onPressed: () {
setState(() {
interestList[_list[index]] = false;
print("$index");
_list.removeAt(index);
});
},
)
],
),
)),
ListView(
shrinkWrap: true,
children: interestList
.map((key, value) {
// isInterest[key] = true;
return MapEntry(
key,
ListTile(
title: Text(key),
trailing: Checkbox(
value: interestList[key],
onChanged: (val) {
setState(() {
interestList[key] = !interestList[key]!;
interestList[key]! ? _list.add(key) : _list.remove(key);
});
},
),
));
})
.values
.toList(),
),
],
)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/395282.html
上一篇:錯誤:無法無條件呼叫方法“[]”,因為接收方可以為“null”。嘗試使呼叫有條件(使用'?.')
下一篇:如何在顫振中鏡像路徑?
