1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

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

Discussão em 'Mobile' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    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
    }))

    Continue reading...

Compartilhe esta Página