[英]checkbox in listView.separated in flutter
我正在嘗試在列表視圖 flutter 中做復選框,但是當我 select 全部被選中時,我只想 select 我點擊的不是全部。 另外,我怎么知道選擇了哪些項目
這是我的代碼:
bool value = false;
ListView.separated(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemBuilder: (context, index) => Container(
height: 100,
width: double.infinity,
decoration: BoxDecoration(
border: Border.all(
color: Colors.grey, width: 1),
),
child: ListTile(
title: Column(
mainAxisAlignment:
MainAxisAlignment.start,
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Row(
children: [
Text(list[index].name),
SizedBox(width: 10),
CheckboxListTile(
value: value,
onChanged:
(bool value) {
this.value = value;
},
)
],
),
],
),
),
),
separatorBuilder: (context, index) =>
SizedBox(
height: 5,
),
itemCount: 5,
)
您正在使用單個 bool 到 select 列表。 這就是為什么您會出現這種行為。 您可以創建一個空列表來提取所選項目。
您可以按照此示例代碼
class ChWL extends StatefulWidget {
const ChWL({super.key});
@override
State<ChWL> createState() => _ChWLState();
}
class _ChWLState extends State<ChWL> {
List<int> list = List.generate(33, (index) => index);
List<int> selectedItem = [];
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) => CheckboxListTile(
title: Text("${list[index]}"),
value: selectedItem.contains(list[index]),
onChanged: (value) {
bool isChecked = selectedItem.contains(list[index]);
if (isChecked) {
selectedItem.remove(list[index]);
} else {
selectedItem.add(list[index]);
}
setState(() {});
},
),
),
);
}
}
Row( children: [ Text(list[index].name), SizedBox(width: 10), CheckboxListTile( value: value, onChanged: (bool value) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.