简体   繁体   English

Flutter - CheckboxListTile - 覆盖标题样式

[英]Flutter – CheckboxListTile - Override title style

In a CheckboxListTile, when we check the tile, the check box and the title change their color.在 CheckboxListTile 中,当我们选中 tile 时,复选框和标题会改变颜色。 What I need to achieve is to split the behavior and keep my title widget with the default color.我需要实现的是拆分行为并使我的标题小部件保持默认颜色。

In this example, I'm trying to replace the title color but the default behavior is still in place and change the title color the same way as the checked icon:在此示例中,我尝试替换标题颜色,但默认行为仍然存在,并以与选中图标相同的方式更改标题颜色:

https://dartpad.dev/?id=7f55df6ce3ba094cfe1db680ccc4b400 https://dartpad.dev/?id=7f55df6ce3ba094cfe1db680ccc4b400

The only way I found to achieve the goal is to set directly in the widget the color.我发现实现目标的唯一方法是直接在小部件中设置颜色。

Text(
    'Select Example',
    style: TextStyle(color: Colors.red),
  )

I know I can do a custom item do reach this objective but my question is if possible to override the title widget color when the tile is selected in the CheckboxListTile Flutter widget?我知道我可以做一个自定义项目来达到这个目标,但我的问题是,当在 CheckboxListTile Flutter 小部件中选择磁贴时,是否可以覆盖标题小部件颜色?

Thanks for reading!谢谢阅读!

Its also weird for me that there are no separate properties for checkbox and text theme, but this is how you can achieve it.对我来说也很奇怪,复选框和文本主题没有单独的属性,但这就是实现它的方法。

return CheckboxListTile(
      title: DefaultTextStyle(
        style: const TextStyle(color: Colors.white),
        child: widget.child,
      ),
      tileColor: Colors.black,
      selectedTileColor: Colors.black,
      selected: isSelected,
      dense: true,
      value: isSelected,
      onChanged: (v) => setState(() => isSelected = !isSelected),
      controlAffinity: ListTileControlAffinity.leading,
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM