2

All checkboxes were selected when I select only one checkbox. I tried most of solves in stackoverflow's post but those solves did not work for my code. Checkbox never be selected when I click one box Because value of isSelected inside Controller never change to true.

my model


class Sites {
  String? id;
  String? code;
  String? name;
  String? status;
  Timestamp? lastClickTime;
  int count;
  bool isSelected;
  

  Sites(
    this.id,
    this.code,
    this.name,
    this.status,
    this.lastClickTime,
    this.count,
    this.isSelected
  );
  
}

my controller

class SitesControllers extends GetxController {

  List<Sites> sitesList = <Sites>[].obs;
  bool isSelected = false;

  Future<void> getSites (String number) async {
    try {
      QuerySnapshot sites = await FirebaseFirestore.instance.collection("subscribers").doc(number).collection("sites").get();
      sitesList.clear();
      for(final site in sites.docs){
        sitesList.add(
          Sites(
            site.id,
            site['code'],
            site['name'],
            site['status'],
            site['lastClickTime'],
            0,
            isSelected
          )
        );
      }
      update();
    } catch (e) {
      Get.snackbar(
        "SitesControllers, ERROR", "> $e", 
        snackPosition: SnackPosition.BOTTOM);
    }
  }
}

my homepage.view


class HomePage extends StatelessWidget {
  String phoneNumber;
  HomePage({super.key, required this.phoneNumber});

  SitesControllers sitesControllers = Get.put(SitesControllers());
  CountController countController = Get.put(CountController());

  int actionController = 0;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SitesControllers>(
        init: SitesControllers(),
        initState: (_) {},
        builder: (sitesControllers) {
          sitesControllers.getSites(phoneNumber);
          return Scaffold(
            backgroundColor: Colors.white,
            appBar: buildAppBarField("Home Page", false),
            body: buildSiteField(context, phoneNumber),
            floatingActionButtonLocation:
                FloatingActionButtonLocation.centerDocked,
            floatingActionButton: buildFloatActionButtonField(),
            bottomNavigationBar: buildBottomNavigationBarField(),
          );
        });
  }

  Widget buildSiteField(BuildContext context, String number) {
    return Padding(
      padding: EdgeInsets.symmetric(vertical: context.dynamicHeight(0.02)),
      child: Obx(() => ListView.builder(
          itemCount: sitesControllers.sitesList.length,
          itemBuilder: (BuildContext context, index) {
            //countController.counter(number, sitesControllers.sitesList[index].id.toString());
            return Padding(
              padding: EdgeInsets.symmetric(
                  horizontal: context.dynamicWidth(0.04),
                  vertical: context.dynamicHeight(0.005)),
              child: Container(
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(15),
                      border: Border.all(
                          color: ColorConstants.mainColor, width: 2)),
                  child: ListTile(
                    visualDensity: VisualDensity.compact,
                    dense: true,
                    isThreeLine: true,
                    onTap: () { buildListTileOnTapField(index, number); },
                    leading: buildListTileLeadingField(
                        context,
                        sitesControllers.sitesList[index].name.toString(),
                        index),
                    subtitle: buildListTileSubstitleField(index),
                    trailing: buildListTileTrailingField(context, index),
                  )),
            );
          })),
    );
  }

  // WIDGETS
  Widget buildListTileLeadingField(BuildContext context, String siteName, int index) {
    return actionController == 1
        ? Checkbox(
            checkColor: Colors.white,
            value: sitesControllers.sitesList[index].isSelected,
            onChanged: (bool? value) {
              sitesControllers.sitesList[index].isSelected = !sitesControllers.sitesList[index].isSelected;
            },
          )
        : Padding(
            padding:
                EdgeInsets.symmetric(vertical: context.dynamicHeight(0.007)),
            child: Text(
              siteName.substring(0, 1).toUpperCase(),
              style: const TextStyle(
                  color: ColorConstants.siteFirstLetter,
                  fontSize: 25,
                  fontWeight: FontWeight.bold),
            ),
          );
  }

 ...

}

Ozan
  • 53
  • 6
  • You could modify your buildListTileLeadingField function to also take 'index' as a parameter, that way in your onChanged parameter for the checkbox you can update the value of the checkbox for the index that you receive from the itemBuilder property of the listview builder – Vipin Kumar Kashyap Apr 16 '23 at 06:49

0 Answers0