繁体   English   中英

如何更改 CheckBoxListTile 中的值项?

[英]How change value item in CheckBoxListTile?

我正在尝试更新 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 的新手。

这是我在我的应用程序中使用的代码,希望它会对你有所帮助

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);
                                  });
                                }
                              },
                            );

Angular 有一个惊人的文档,详细教授每个概念 (60%)。 我强烈建议您访问文档站点。 每个主题都有示例和解释。

https://angular.io/guide/reactive-forms

非常感谢你提出这么漂亮的问题。 Angular 是爱。

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;
  }
}

这基本上做的是,它使用测试来保存小部件 state。 在您的代码中,测试对于您的构建 function 是本地的,并且会在构建 function 运行时重新初始化。

这样,当调用 setState() 时,它不会在小部件重建时重新初始化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM