7

I want to build a checkbox with CheckboxListTile inside this widget dialog but when I tap the checkbox the checked on the checkbox doesn't change.

This is my code:

Future<Null> _showGroupDialog(BuildContext context) async {
    await showDialog(
        context: context,
        builder: (BuildContext dialogContext) =>
            Dialog(child: _buildCheckboxGroups(context)));
}

Widget _buildCheckboxGroups(BuildContext context) {
    List<Widget> childrens = List.generate(_groups.length, (index) {
      return CheckboxListTile(
        title: Text(_groups[index].name),
        value: _groups[index].checked,
        onChanged: (bool val) {
          setState(() {
            _groups[index].checked = val;
          });
        },
      );
    });

    return Container(
        child: Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: childrens,
    ));
}

Btw, the onChange method is invoked when I tap the checkbox. Can anyone solve this?

1

1 Answer 1

3
class _MyHomePageState extends State<MyHomePage>{

 //<Here you have to set default value for _groups[index].checked to false/true>
 @override
 Widget _buildCheckboxGroups(BuildContext context) {
  List<Widget> childrens = List.generate(_groups.length, (index) {
    return CheckboxListTile(
    title: Text(_groups[index].name),
    value: _groups[index].checked,
    onChanged: (bool val) {
      setState(() {
        _groups[index].checked = val;
      });
    },
  );
});}

Working Example is given below

class _MyHomePageState extends State<MyHomePage> {
 bool flagWarranty=false;
 @override
 Widget build(BuildContext context) {
  return Scaffold(
  appBar: AppBar(title: Text(widget.title)),
  body: new Container(
      padding: new EdgeInsets.all(20.0),
      child: new Form(
        key: this._formKey,
        child: new ListView(
          children: <Widget>[
              new Checkbox(
              value: flagWarranty,
              onChanged: (bool value) {
                setState((
                    ) {
                  flagWarranty=value;
                });
              },
            )
       ],),)));
 }}
Sign up to request clarification or add additional context in comments.

2 Comments

Please always add descriptions about your code. Thank you.
since onChecked requires a nullable bool? this example doesn`t even pass the syntax check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.