我正在尝试更新 CheckBoxListTile 的检查,但没有得到想要的结果。
这是我的代码:
Widget build(BuildContext context) {
return Query(
options: QueryOptions(document: getVehiclesTypeQueryDoc),
builder: (result, {fetchMore, refetch}) {
if (result.isLoading && result.data == null) {
return Center(
child: CircularProgressIndicator(),
);
}
newVehiclesType = [];
final _vehiclesTypeJson = result.data['tipoVehiculos'] as List<dynamic>;
for (final i in _vehiclesTypeJson) {
var productMap = {
'id': i['id'],
'nombre': i['nombre'],
'imagenActivo': i['imagenActivo'],
'isSelected': true
};
newVehiclesType.add(productMap);
}
final List<dynamic> test = newVehiclesType;
print('test: $test');
return ListView.builder(
itemCount: test.length,
shrinkWrap: true,
itemBuilder: (context, index) {
print(index);
print('VALO: ${test[index]['isSelected']}');
return CheckboxListTile(
title: Text(test[index]['nombre'].toString()),
value: test[index]['isSelected'],
onChanged: (bool newValue) {
setState(() {
test[index]['isSelected'] = newValue;
});
},
);
},
);
},
);
}
我创建了一个 newVehiclesType 变量,查询没有给我一个变量来使用检查。
我是 Flutter 的新手。
答案 0 :(得分:0)
这是我在我的应用程序中使用的代码,希望对您有所帮助
value: isCheckedEdit.contains(_ingredientList[index].nameIngredient),
onChanged: (value) {
if (value) {
setState(() {
isCheckedEdit
.add(_ingredientList[index].nameIngredient);
print(isCheckedEdit);
});
} else {
setState(() {
isCheckedEdit.remove(
_ingredientList[index].nameIngredient);
print(isCheckedEdit);
});
}
},
);
答案 1 :(得分:0)
Angular 有一个很棒的文档,其中详细介绍了每个概念 (60%)。我强烈建议您访问文档站点。每个主题都有示例和说明。
https://angular.io/guide/reactive-forms
非常感谢你提出这么漂亮的问题。 Angular 就是爱。
答案 2 :(得分:0)
class VehiclesList extends StatefulWidget {
@override
_VehiclesListState createState() => _VehiclesListState();
}
class _VehiclesListState extends State<VehiclesList> {
List<dynamic> test;
@override
Widget build(BuildContext context) {
// your code
// when result has data, instead of
// final List<dynamic> test = newVehiclesType;
// do this
test = newVehiclesType;
}
}
它的基本作用是,它使用测试来保持小部件状态。在您的代码中,测试对于您的构建函数是本地的,并且会在构建函数运行时重新初始化。
这样当 setState() 被调用时,它不会在小部件重建时重新初始化。