我正在使用 CheckboxListTile 实现复选框列表,但遇到了问题。
我正在尝试使用 setState 激活/停用复选框,但它似乎没有更新。
这是我的代码。
这是我调用返回 CheckboxListTitle 的函数的部分。
Column(
children: [
RefreshIndicator(
onRefresh: refetch,
child: ListView.builder(
itemCount: newVehiclesType.length,
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (context, index) {
return _itemVehicle(
newVehiclesType[index]['imagenActivo']['url'].toString(),
newVehiclesType[index]['nombre'].toString(),
newVehiclesType[index]['isSelected'],
(newVehiclesType[index]['id']),
index);
},
),
),
SizedBox(
height: 20,
),
这是我重复使用的 Widget 功能。
Widget _itemVehicle(String assetImage, String vehicleName, bool isSelected,
dynamic index, int indexList) {
var currentIndex = indexList;
return CheckboxListTile(
secondary: Image.network(
'http://192.168.1.3:1337$assetImage',
width: 30,
height: 30,
),
title: Text(vehicleName),
value: newVehiclesType[currentIndex]['isSelected'],
controlAffinity: ListTileControlAffinity.leading,
onChanged: (bool newValue) {
print(newValue);
setState(() {
currentIndex =
newVehiclesType.indexWhere((element) => element['id'] == index);
newVehiclesType[currentIndex]['isSelected'] = newValue;
});
},
);
}