Search code examples
flutterdartflutter-getx

How can make checkbox select and multiple selection with Getx


my code is without button that make select all

View class

GetBuilder<ProductController>(
                              
                              builder: (controller) {
                                
                              
                              return
                            
                            Container(
                              height: 50,
                              child: Transform.scale(
                                scale: 1.2,
                                child:  Checkbox(
                                  activeColor: MyThemes.yellow,
                                  shape:RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(5)
                                  ),
                                  splashRadius: 18.8,
                                  value: controller.items[index]["check"],
                                   onChanged:((value) {
                                    controller.CheckBrand(index, value);
                                  } 
                                )),
                              ),
                            );}),

controller class

    RxList<Map<String,dynamic>> items = [{"one": "Item1", "check": false}, 
  {"one": "Item2", "check": false}, {"one": "Item3", "check": false}].obs;



CheckBrand(index, ischeck){   items[index]["check"]=ischeck;   update(); }

I want to ask is there another way to make checkbox selection and how can I make select all button for this items


Solution

  • you can iterate over elements to check them all at once, you can use this method:

    void checkAll({bool newValue = true}) {
     items.value.forEach((element) {
      element["check"] = newValue;
     });
    
    update();
    }
    

    I will add also a method that uncheck all at once:

    void unCheckAll() { 
     checkAll(newValue: false);
    }
    

    now from your view, just call it like this:

    controller.checkAll(); // will check all check boxes
    controller.unCheckAll(); // will uncheck all check boxes