简体   繁体   English

如何更改 CheckBoxListTile 中的值项?

[英]How change value item in CheckBoxListTile?

I am trying to update the check of the CheckBoxListTile but I am not getting the desired result.我正在尝试更新 CheckBoxListTile 的检查,但没有得到想要的结果。

This is my code:这是我的代码:

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

I created a newVehiclesType variable, the query did not give me a variable to use the check for.我创建了一个 newVehiclesType 变量,查询没有给我一个变量来使用检查。

I am new to flutter.我是 flutter 的新手。

this is the code that i use in my application hopefully hopefully it will help you这是我在我的应用程序中使用的代码,希望它会对你有所帮助

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 has an amazing documentation that teaches every concept in detail (60%). Angular 有一个惊人的文档,详细教授每个概念 (60%)。 I highly recommend you to visit the the documentation site.我强烈建议您访问文档站点。 There are examples and explanations for each topic.每个主题都有示例和解释。

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

Thank you so much for bringing such a beautiful question up.非常感谢你提出这么漂亮的问题。 Angular is love. 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;
  }
}

What this basically does is, it uses test to hold the widget state.这基本上做的是,它使用测试来保存小部件 state。 In your code, test is local to your build function and would be reinitialized as many times build function runs.在您的代码中,测试对于您的构建 function 是本地的,并且会在构建 function 运行时重新初始化。

This way it would not reinitialize on widget rebuilds when setState() is called.这样,当调用 setState() 时,它不会在小部件重建时重新初始化。

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

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