[英]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
)如果我为
Radio
的onChanged
属性提供了一个虚拟函数 - 它会变为活动状态,但问题是我不希望它可点击,我希望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:当前结果:
Result with onChange
(I can't uncheck the radio when clicking on it): onChange
结果(单击它时我无法取消选中收音机):
Radio
uses unselectedWidgetColor
of ThemeData
. Radio
使用unselectedWidgetColor
的ThemeData
。 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).如果没有回调传入
onChanged
到Radio
,则将其解释为禁用(这适用于许多默认材质小部件)。
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.