How do I change the tick color for CheckboxListTile

Viewed 8207

I have my checkbox widget all set up and would like to change the tick color to green when selected (currently it is white). So I managed to change the color of the checkbox when it is un-selected to white by adding a theme. I want to change the selected tick color to green, however I cant seem to find the right option under theme to do so.

Code:

  Widget buildResultTile(data) {
    return Theme(
      data: ThemeData(unselectedWidgetColor: white),
      child:
        CheckboxListTile(
        activeColor: transparent,
        title: AutoSizeText(
          data,
          maxLines: 1,
          style: TextStyle(
            color: white,
          ),
        ),
        value: _serachSelectList.contains(data),
        onChanged: (bool value) {
          setState(() {
            if (value) {
              _serachSelectList.add(data);
            } else {
              _serachSelectList.remove(data);
            }
          });
        },
        secondary: const Icon(Icons.account_box, color: white),      
      )
    );
  } 

Un-selected:

enter image description here

Selected (I want only the tick to be Colors.green):

enter image description here

5 Answers

To change the fill color of the checkbox in a CheckboxListTile, you can simply set the toggleableActiveColor property in your ThemeData:

ThemeData(
    // ... other theme values ...

    toggleableActiveColor: Colors.green
)

Changing the color of the tick mark is unfortunately impossible with CheckboxListTile since it doesn't expose the checkColor property of its Checkbox widget, so you are stuck with rolling your own list tile.

You need to ditch the CheckboxListTile and instead use a Row with an icon, text, and a simple Checkbox widget.

Checkbox provides checkColor property - which is responsible for the check/tick color and is white by default. Set the color you desire for that property and it should work.

e.g.

Row(crossAxisAlignment: CrossAxisAlignment.center, children: [

  Icon(Icons.account_box, color: Colors.white),
  Expanded(
    child: AutoSizeText(
      data,
      maxLines: 1,
      style: TextStyle(
        color: white,
      ),
    )
  ),
  Checkbox(
    value: _serachSelectList.contains(data),
    onChanged: (bool value) {
      setState(() {
        if (value) {
          _serachSelectList.add(data);
          setState((){ default_color = selected_color });
        } else {
          _serachSelectList.remove(data);
          setState((){ default_color = unselected_color });
        }
      });
    },
    checkColor: Colors.green,
    activeColor: Colors.transparent,
    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
  ),

]);

I did not test this code - you might encounter size & alignment issues that you would need to solve yourself. Nevertheless, the general idea is valid.

Let me know if this helped.

please try this pesudo code,

Color selected_color = Colors.green;
Color unselected_color = Colors.transparent;
Color default_color = unselected_color ;

  Widget buildResultTile(data) {
    return Theme(
      data: ThemeData(unselectedWidgetColor: white),
      child:
        CheckboxListTile(
        activeColor: default_color,
        title: AutoSizeText(
          data,
          maxLines: 1,
          style: TextStyle(
            color: white,
          ),
        ),
        value: _serachSelectList.contains(data),
        onChanged: (bool value) {
          setState(() {
            if (value) {
              _serachSelectList.add(data);
              setState((){ default_color = selected_color });
            } else {
              _serachSelectList.remove(data);
              setState((){ default_color = unselected_color });
            }
          });
        },
        secondary: const Icon(Icons.account_box, color: white),      
      )
    );
  } 

i hope to help you

it's working code for checkbox color change for flutter

checkColor: Colors.white,

activeColor: Colors.red,

CheckboxListTile(
                checkColor: Colors.white,
                activeColor: Colors.red,
                value: checkedValue,
                onChanged: (newValue) {
                  setState(() {
                    checkedValue = newValue!;
                  });
                },
                controlAffinity:
                    ListTileControlAffinity.leading, //  <-- leading Checkbox
              ),
Related