John Joe
John Joe

Reputation: 12803

Display checkbox in listView flutter

Please help ! I have two checkbox in table row. First checkBox is clickable but the second are not clickable.

 ListView.builder(
                    shrinkWrap: true,
                    physics: const NeverScrollableScrollPhysics(),
                    itemCount: widget.checklist['sections'].length,
                    itemBuilder: (context, index) {
                      final item = widget.checklist['sections'][index];

                      String s = item['applicable'];
                      var a = s.split(",");

                      return Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Table(
                                columnWidths: {
                                  0: FixedColumnWidth(
                                      250.0), // fixed to 100 width
                                  1: FlexColumnWidth(),
                                  2: FlexColumnWidth(), //fixed to 100 width
                                },
                                defaultColumnWidth: FixedColumnWidth(120),
                                border: TableBorder.all(),
                                children: [
                                  TableRow(children: [
                                    TableCell(
                                        child: Container(
                                            color: Colors.grey.shade300,
                                            child: Padding(
                                                padding: EdgeInsets.all(10),
                                                child: Text(item['name'])))),
                                    for (var i in a)
                                      TableCell(
                                          child: Container(
                                              color: Colors.grey.shade300,
                                              child: Padding(
                                                  padding: EdgeInsets.all(10),
                                                  child: Text(i))))
                                  ]),
                                  ...item['questions']
                                      .asMap()
                                      .entries
                                      .map((items) {
                                    print(items.key);
                                    return TableRow(children: [
                                      Padding(
                                          padding: EdgeInsets.all(10),
                                          child: Text(items.value['name'])),
                                      Checkbox(
                                       tristate: true,
                                       value: true,
                                        onChanged: (bool value) {
                                          setState(() {
                                            widget.checklist['sections']
                                                [items.keys] = value;
                                          });
                                        },
                                      ),
                                      Checkbox(onChanged: null, value: false)
                                    ]);
                                  }).toList()
                                ])
                          ]);
                      // );
                    })

But I get this error

'package:flutter/src/material/checkbox.dart': Failed assertion: line 76 pos 15: 'tristate || value != null': is not true.

Upvotes: 0

Views: 1870

Answers (2)

kounex
kounex

Reputation: 1715

Based on your error, which states that either tristate is not true (which is correct since it defaults to false and you did not set it on your own) or value == null (which will most likely be the case here).

Your Checkbox implementation used this as the value:

Checkbox(
  value: values[items.key],
  ...
),

Your values map does not have one of the items.key as a key. I would recommend you to debug this part or actually set tristate to true and see where your checkbox does indeed have the extra state (will be displayed as "-" in the box).

From there you might figure out which key is not part of your values map!

Upvotes: 1

Huzaifa Ahmed
Huzaifa Ahmed

Reputation: 324

Try this CheckboxListTile from flutter itself

https://api.flutter.dev/flutter/material/CheckboxListTile-class.html

Upvotes: 0

Related Questions