简体   繁体   English

如何在 Flutter 中更改收音机的非活动颜色?

[英]How to change radio's inactive color in Flutter?

I have a Radio button inside a ListTile .我在ListTile有一个Radio按钮。 When the ListTile is clicked, I change the Radio 's value.单击ListTile ,我更改Radio的值。 I don't want the radio to be clickable, so I don't provide an onChanged callback:我不希望收音机可点击,所以我不提供onChanged回调:

ListTile(
  onTap: () => onChanged(template.id),
  leading: Radio(
    value: template.id,
    groupValue: checkedId,
  )
  ...
)

Doing that, the Radio becomes "inactive" and changes it's color to grey.这样做后, Radio将变为“不活动”并将其颜色更改为灰色。 The Radio has an activeColor property, but not for inactive. Radio有一个activeColor属性,但不是用于非活动的。

If I provide a dummy function to Radio 's onChanged property - it becomes active, but the problem is I don't want it to be clickable, I want the ListTile to be clickable only (the reason is - I want to be able to uncheck the Radio )如果我为RadioonChanged属性提供了一个虚拟函数 - 它会变为活动状态,但问题是我不希望它可点击,我希望ListTile仅可点击(原因是 - 我希望能够取消选中Radio

Also, I only want to change the inactive color of those specific Radio buttons, and not for the whole app.此外,我只想更改那些特定Radio按钮的非活动颜色,而不是整个应用程序。

Current Result:当前结果:

演示 1

Result with onChange (I can't uncheck the radio when clicking on it): onChange结果(单击它时我无法取消选中收音机):

演示 2

Radio uses unselectedWidgetColor of ThemeData . Radio使用unselectedWidgetColorThemeData If you need to change it only for a few radios on a specific screen, wrap them in Theme widget to override a color:如果您只需要为特定屏幕上的几个收音机更改它,请将它们包装在Theme小部件中以覆盖颜色:

Theme(
  data: Theme.of(context).copyWith(
    unselectedWidgetColor: Colors.red,
    disabledColor: Colors.blue
  ),
  child: Column(
    children: <Widget>[
      ListTile(
        onTap: () => setState(() => value = 0),
        leading: Radio(
          value: 0,
          groupValue: value,
          onChanged: (v) => setState(() => value = v),
        )
      ),
      ListTile(
        onTap: () => setState(() => value = 1),
        leading: Radio(
          value: 1,
          groupValue: value,
          onChanged: (v) => setState(() => value = v),
        )
      ),
    ],
  ),
)

If no callback passed in onChanged to Radio , it is interpreted as disabled (this works for many default material widgets).如果没有回调传入onChangedRadio ,则将其解释为禁用(这适用于许多默认材质小部件)。

You can also set the fillColor propery, it resolves a color for multiple radio states.您还可以设置fillColor属性,它为多个无线电状态解析颜色。 In this case, it returns Colors.blue for all states.在这种情况下,它为所有状态返回Colors.blue

Radio(
          fillColor: MaterialStateColor.resolveWith((states) => Colors.blue),
          value: 1,
          groupValue: _optionValue,
          onChanged: changeValue,
        )

To change the radio unselected color:要更改收音机未选择的颜色:

unselectedWidgetColor: Colors.green,

To change radio selected color:要更改收音机选定的颜色:

toggleableActiveColor: Colors.green,

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

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