samedi 20 novembre 2021

Flutter: After searching in the checkbox list, the checkboxes are broken

There is a listview in my Flutter project and a CheckboxListTile inside it.

I am using a search widget that controls this Listview.

My problem is: When I search for a listview element, the result appears and when I reset the search after I set the checkbox value of that listview element to true, I see that the first element of the list is marked, not the element I marked on the screen.

I am attaching a short video below to explain this problem better.

VIDEO LINK YOUTUBE

And also this is my code:

My Search Widget:

  TextEditingController _controller = TextEditingController();
  List<bool>? _isChecked;
  List<String> _selectedItems = <String>[];
  List<Gruplar>? grup;
  String query = '';
  bool _checkInTheBox = false;

 GrupSearch(
                                        text: query,
                                        controller: _controller,
                                        onChanged: (String query) {
                                          final grup =
                                              allGrup.where((element) {
                                            final titleAd =
                                                element.ad!.toLowerCase();
                                            final searchLower =
                                                query.toLowerCase();

                                            return titleAd
                                                .contains(searchLower);
                                          }).toList();
                                          _setState(() {
                                            this.query = query;
                                            this.grup = grup;
                                            this.query = "";
                                          });
                                        }),

My ListView

ListView.builder(
                                          itemCount: grup!.length,
                                          itemBuilder: (context, index) {
                                            final items = grup![index];
                                            return CheckboxListTile(
                                                value: _isChecked![index],
                                                checkColor:
                                                    it_tool_main_blue,
                                                tileColor: Colors.black,
                                                activeColor: Colors.white,
                                                selectedTileColor:
                                                    Colors.black,
                                                title: Text(
                                                  grup![index].ad!,
                                                  style: TextStyle(
                                                      fontSize: MediaQuery.of(
                                                                  context)
                                                              .size
                                                              .width /
                                                          27,
                                                      color:
                                                          it_tool_main_blue,
                                                      fontWeight:
                                                          FontWeight.bold),
                                                ),
                                                onChanged: (val) {
                                                  _setState(() {
                                                    if (!_selectedItems.contains( grup![index].ad)) {
                                                      _selectedItems.add(
                                                          grup![index]
                                                              .ad
                                                              .toString());
                                                      _isChecked![index] =
                                                          val!;
                                                      print(_selectedItems);
                                                    } else {
                                                      _selectedItems.remove(
                                                          grup![index]
                                                              .ad
                                                              .toString());
                                                      _isChecked![index] =
                                                          val!;
                                                      print(_selectedItems);
                                                    }
                                                  });
                                                });
                                          }),

As a result, I want the checkbox I selected after searching to appear correctly after the search is over.

Thanks for your help.




Aucun commentaire:

Enregistrer un commentaire