How to select and unselect all checkboxes based on button click in flutter

I have a listview which contains checkbox inside the listTile. This listTile will be generated dynamically based on the length of the list. Here is my ListTile and Custom checkbox code.

   ListTile(
        leading: Container(
            height: 30,
            width: 42,
            decoration: const BoxDecoration(
                image: DecorationImage(
                    image: AssetImage(
                        "assets/images/ic_subtract.png"),
                    fit: BoxFit.fill)),
            child: const Image(
            image: AssetImage(
                "assets/images/ic_dummy_project_icon.png"),
            width: 24,
            height: 24,
            )),
        title: Text(
            eventTicketList[index]
                .firstName
                .toString(),
            style: const TextStyle(
                color: Color.fromRGBO(
                    26, 49, 60, 1),
                fontWeight: FontWeight.w500,
                fontSize: 16,
                fontFamily: 'SF Pro Text')),
        subtitle: loadTicketInfo(
            eventTicketList, index),
        trailing: loadCheckBox(isChecked))
 loadCheckBox(bool isChecked) {
    return CustomCheckbox(
      value: isChecked,
      onChanged: (value) {
        setState(() {
          isChecked = value!;
        })
      },
    );
class CustomCheckbox extends StatefulWidget {
  const CustomCheckbox({
    required this.value,
    Key? key,
    this.width = 24.0,
    this.height = 24.0,
    this.color,
    this.iconSize,
    this.onChanged,
    this.checkColor,
  }) : super(key: key);

  final double width;
  final double height;
  final Color? color;
  final bool? value;
  // Now you can set the checkmark size of your own
  final double? iconSize;
  final Color? checkColor;
  final Function(bool?)? onChanged;

  @override
  State<CustomCheckbox> createState() => _CustomCheckboxState();
}

class _CustomCheckboxState extends State<CustomCheckbox> {
  bool isChecked = false;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        setState(() => isChecked = !isChecked);
        widget.onChanged?.call(isChecked);
      },
      child: Container(
          width: widget.width,
          height: widget.height,
          child: isChecked
              ? Image(
            image: AssetImage("assets/images/ic_checked.png"),
            height: 24,
            width: 24,
            alignment: Alignment.topRight,
          )
              : Image(
            image: AssetImage("assets/images/ic_selector.png"),
            height: 24,
            width: 24,
            alignment: Alignment.topRight,
          )
      ),
    );
  }
}

I want to select all the check box when i click the button and I also want to unselect all when I click the same button again. I have tried few examples from stackoverflow and from different other sources. But nothing works fine or I have not done that properly. Could someone help me to solve this problem?

 Padding(
    padding: EdgeInsets.only(right: 25),
    child: TextButton(
        child: const Text("Select All",
            style: TextStyle(
                fontSize: 16,
                fontFamily: 'SF Pro Text',
                fontWeight: FontWeight.w200,
                color: Color.fromRGBO(
                    0, 94, 180, 1))),
        onPressed: () {
          //select all checkbox
        }))

All you need to do is to override didUpdateWidget inside CustomCheckbox widget. Because CustomCheckBox widget is stateful widget, you don’t get it’s initState called multiple times to set isChecked. DidUpdateWidget gets called followed by build method.

@override
  void didUpdateWidget(covariant CustomCheckbox oldWidget) {
    super.didUpdateWidget(oldWidget);

    if (widget.value != oldWidget.value) {
      isChecked = widget.value!;
    }
  }

Here is the full code which I tried

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      home: const HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _isAllChecked = false;

  final List<String> _items = const [
    'London',
    'Paris',
    'New York',
    'Berlin',
    'Madrid'
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Checkboxes'),
        actions: [
          Checkbox.adaptive(
              value: _isAllChecked,
              onChanged: (value) {
                if (value == null) {
                  return;
                }
                setState(() {
                  _isAllChecked = value;
                });
              }),
        ],
      ),
      body: ListView.builder(
        itemBuilder: (context, index) {
          return ListTile(
              title: Text(
                _items[index],
              ),
              trailing: loadCheckBox(_isAllChecked));
        },
        itemCount: _items.length,
      ),
    );
  }

  loadCheckBox(bool isChecked) {
    return CustomCheckbox(
      value: isChecked,
      onChanged: (value) {
        setState(() {
          isChecked = value!;
        });
      },
    );
  }
}

class CustomCheckbox extends StatefulWidget {
  const CustomCheckbox({
    required this.value,
    super.key,
    this.width = 24.0,
    this.height = 24.0,
    this.color,
    this.iconSize,
    this.onChanged,
    this.checkColor,
  });

  final double width;
  final double height;
  final Color? color;
  final bool? value;
  // Now you can set the checkmark size of your own
  final double? iconSize;
  final Color? checkColor;
  final Function(bool?)? onChanged;

  @override
  State<CustomCheckbox> createState() => _CustomCheckboxState();
}

class _CustomCheckboxState extends State<CustomCheckbox> {
  bool isChecked = false;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        setState(() => isChecked = !isChecked);
        widget.onChanged?.call(isChecked);
      },
      child: SizedBox(
        width: widget.width,
        height: widget.height,
        child: isChecked
            ? const Icon(Icons.check_box)
            : const Icon(Icons.check_box_outline_blank),
      ),
    );
  }

  @override
  void didUpdateWidget(covariant CustomCheckbox oldWidget) {
    super.didUpdateWidget(oldWidget);

    if (widget.value != oldWidget.value) {
      isChecked = widget.value!;
    }
  }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật