You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter如何修改RadioGroup<String>.builder选项文本颜色

group_radio_button 单选选项文本颜色修改方法

你代码里加的activeColor只能改单选圆点的激活颜色,管不到选项文本。要改文本颜色直接给RadioButtonBuilder传textStyle参数就行,还能根据选中状态分别设不同颜色。

改法

  • 找到itemBuilder里返回RadioButtonBuilder的代码块
  • 加textStyle属性,判断当前渲染的选项是不是等于已选的_verticalGroupValue,对应给选中/未选状态设不同色值就行
  • 要是想所有选项文本统一一个颜色,不用加判断,直接给color传固定色值就ok

改完的完整代码

final List<String> _statusUpdate = ["Lunch", "Meeting", "Available"];

SizedBox(
  height: 150,
  child: Padding(
    padding: const EdgeInsets.only(top: 30.0, left: 60.0, bottom: 20.0),
    child: Column(
      children: <Widget>[
        Transform.scale(
          scale: 1.4,
          child: RadioGroup<String>.builder(
            groupValue: _verticalGroupValue,
            onChanged: (value) => setState(() {
              _verticalGroupValue = value!;
              print("click value : " + value + _verticalGroupValue);
              _saveSwitch();
            }),
            items: _statusUpdate,
            itemBuilder: (item) => RadioButtonBuilder(
              item,
              // 这里配置文本样式
              textStyle: TextStyle(
                // 选中项用绿色,未选中用87%透明度黑色,色值可按需替换
                color: item == _verticalGroupValue ? Colors.green : Colors.black87,
                fontSize: 15,
              ),
            ),
            activeColor: Colors.green,
          ),
        ),
      ],
    ),
  ),
)

要调整字重、字号、行高等其他文本属性,直接在textStyle里加对应参数就能生效,不需要额外套外层组件。

内容的提问来源于stack exchange,提问作者DGAD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 09:21:34