samedi 29 avril 2023

How to save checkbox value from list in flutter?

I have listview with checkbox, I saved different products with ids in the list.

When checkbox value is true, product will save in that list and when checkbox value is false, product will remove from that folder.

I want to save the checkbox value is true if the product id is already inside list folder.

Is there any way to do it?

This is some part of the code-

List<int> _selected_box = [];
List<int> selectedProductIds = [];

Listview builder-

'snapshot.data!.data[index].id' = id of folder in list

'widget.prod' = id of products which save in folder

ListView.builder(
                                  itemCount: snapshot.data!.data.length,
                                  itemBuilder: (context, index){
                                    bool isSelected = selectedProductIds.contains(snapshot.data!.data[index].id);
                                    return CheckboxListTile(
                                      title: Text(
                                        snapshot.data!.data[index].name,
                                        maxLines: 1,
                                        overflow: TextOverflow.ellipsis,
                                        style: TextStyle(color: kblue, fontSize: 20.sp, fontWeight: FontWeight.bold),
                                      ),
                                      activeColor: kblue,
                                      value: _selected_box.contains(index),
                                      onChanged: (value) {
                                        setState(() {
                                          // Check if index is already selected
                                          if (_selected_box.contains(index)) {
                                            _selected_box.remove(index);
                                            selectedProductIds.remove(int.parse(snapshot.data!.data[index].id));
                                            futureRemoveMenuData = fetchRemoveMenuData(snapshot.data!.data[index].id, widget.prod);
                                            futureDeleteMenu = fetchDeleteMenu(widget.prod);
                                            futureCreateMenu = fetchCreateMenu();
                                            Fluttertoast.showToast(
                                              msg: "Item Removed from Menu!", // your toast message
                                              toastLength: Toast.LENGTH_SHORT, // duration of the toast
                                              gravity: ToastGravity.BOTTOM, // toast gravity
                                              backgroundColor: Colors.black54, // bFackground color of the toast
                                              textColor: Colors.white, // text color of the toast
                                            );
                                          } else {
                                            _selected_box.add(index);
                                            selectedProductIds.add(int.parse(snapshot.data!.data[index].id));
                                            futureAddMenuData = fetchAddMenuData(snapshot.data!.data[index].id, widget.prod);
                                            futureCreateMenu = fetchCreateMenu();
                                            Fluttertoast.showToast(
                                              msg: "Item Added to Menu!", // your toast message
                                              toastLength: Toast.LENGTH_SHORT, // duration of the toast
                                              gravity: ToastGravity.BOTTOM, // toast gravity
                                              backgroundColor: Colors.black54, // background color of the toast
                                              textColor: Colors.white, // text color of the toast
                                            );
                                          }

                                          // Update the futureAddMenuData based on the new selection
                                          if (value == true) {
                                            futureAddMenuData = fetchAddMenuData(snapshot.data!.data[index].id, widget.prod);
                                          }
                                          else{
                                            futureRemoveMenuData = fetchRemoveMenuData(snapshot.data!.data[index].id, widget.prod);
                                            futureDeleteMenu = fetchDeleteMenu(widget.prod);
                                          }
                                        });
                                      },
                                      contentPadding: EdgeInsets.zero,
                                      controlAffinity: ListTileControlAffinity.leading,  //  <-- leading Checkbox
                                    );
                                  }
                              );

Here is the output-

enter image description here




Aucun commentaire:

Enregistrer un commentaire