如何更改DropdownButton的图标颜色?Flutter

7
在Flutter中,我试图将DropdownButton的图标(向下箭头图标)的颜色更改为白色。我尝试使用style属性,但没有帮助。文本颜色变为白色,但图标仍然是默认的灰色。
DropdownButton(
         style: TextStyle(color: Colors.white, decorationColor: 
             Colors.white),
         items: this.items,
         value: null,
         hint: Text(SaveOptions[_saveOption], style: TextStyle(color: 
             Colors.white)),
         onChanged: (selectedOption) {
           setState(() {
             _saveOption = selectedOption;
           });
         })

我该如何将箭头图标的颜色更改为白色?

7个回答

16

您可以按照以下方式使用字段iconEnabledColoriconDisabledColor

final myDropDownMenu = DropdownButton<String>(
      iconEnabledColor: Colors.white,
      iconDisabledColor: Colors.white,
      value: myInitialValue,
      // The rest of your code
);

7

由于DropdownButton获取最近的Theme中的颜色,因此您有两个选项。

第一个选项是更改应用程序主题的亮度。

另一个选项是通过将您的dropdown按钮包装在一个新的具有暗色brightnessTheme中。

Theme(
   data: Theme.of(context).copyWith(brightness: Brightness.dark),
   child: DropdownButton(
     style: TextStyle(color: Colors.white, decorationColor: Colors.white),
     items: this.items,
     value: null,
     hint: Text(SaveOptions[_saveOption], style: TextStyle(color: Colors.white)),
     onChanged: (selectedOption) {
       setState(() {
         _saveOption = selectedOption;
       });
     },
   ),
 )

3
这有点像是一个hack,但它让你完全控制下拉菜单折叠后的外观。简而言之,将value:null、hint:null、iconsize:null,创建一个包含两个尺寸相同的容器的堆栈:一个显示折叠的下拉菜单,另一个侦测手势“展开”。
class MyDropdownFilled extends StatefulWidget {
  final List<String> dropDownValues;

  const MyDropdownFilled({Key key, @required this.dropDownValues})
      : super(key: key);

  List<DropdownMenuItem<String>> getDropDownMenuItems() {
    return dropDownValues
        .map((itemString) =>
            DropdownMenuItem(child: Text(itemString), value: itemString))
        .toList();
  }

  @override
  _MyDropdownFilledState createState() => _MyDropdownFilledState();
}

class _MyDropdownFilledState extends State<MyDropdownFilled> {
  String _activeDropdown;

  @override
  initState() {
    super.initState();
    _activeDropdown = widget.dropDownValues[0];
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Container(
          width: double.infinity,
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              color: primaryColor.shade600,
              borderRadius: BorderRadius.all(Radius.circular(2))),
          child:
              Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            Text(_activeDropdown, style: Theme.of(context).textTheme.caption),
            Icon(Icons.arrow_drop_down, size: 30, color: Colors.white),
          ]),
        ),
        Container(
          width: double.infinity,
          padding: EdgeInsets.all(10.0),
          decoration: BoxDecoration(
              color: Colors.transparent,
              borderRadius: BorderRadius.all(Radius.circular(2))),
          child: DropdownButtonHideUnderline(
              child: DropdownButton<String>(
            value: null,
            isDense: true,
            iconSize: 0,
            hint: null,
            onChanged: (String newValue) {
              setState(() {
                _activeDropdown = newValue;
              });
            },
            items: widget.dropDownValues.map((String value) {
              return DropdownMenuItem(
                value: value,
                child: Text(value),
              );
            }).toList(),
          )),
        )
      ],
    );
  }
}

0

将您的小部件包装在一个新主题周围,该主题具有您想要设置的值,鉴于您可以查看源代码并查看它从主题中使用的颜色。


0

目前 DropdownButton 的箭头颜色是硬编码的:

  Color get _downArrowColor {
    // These colors are not defined in the Material Design spec.
    if (_enabled) {
      if (Theme.of(context).brightness == Brightness.light) {
        return Colors.grey.shade700;
      } else {
        return Colors.white70;
      }
    } else {
      if (Theme.of(context).brightness == Brightness.light) {
        return Colors.grey.shade400;
      } else {
        return Colors.white10;
      }
    }
  }

您可以创建自己的小部件来自定义此属性。


0

看起来Flutter应该有一种方法来做到这一点,但我认为目前还不可能。我处理这个问题的方法是将“value”设置为null,“iconSize”设置为0,并根据所选内容动态生成“hint”。这样做可以让您完全控制提示小部件。

DropdownButton<int>(
  value: null,
  iconSize: 0,
  hint: Row(
    children: <Widget>[
      Text(_selected,
        style: TextStyle(
          color: Colors.white,
          fontWeight: FontWeight.w700,
        ),
      ),
      Padding(
        padding: EdgeInsets.only(left: 5),
        child: Icon(
          FontAwesomeIcons.caretDown,
          color: Colors.white,
          size: 20,
        ),
      ),
    ],
  ),
  items: dateRanges.map((Map<String, dynamic> value) {
    return DropdownMenuItem<int>(
      value: value['type'],
      child: Text(
        value['name'],
        style: TextStyle(
          color: Colors.grey[800],
          fontWeight: FontWeight.w700,
        ),
      ),
    );
  }).toList(),
  onChanged: (type) => _onDateRangeTypeChanged(type),
)

希望这能有所帮助。

0

前往DropdownButton类 编辑此代码

if (!DropdownButtonHideUnderline.at(context)) {
      final double bottom = widget.isDense ? 0.0 : 8.0;
      result = Stack(
        children: <Widget>[
          result,
          Positioned(
            left: 0.0,
            right: 0.0,
            bottom: bottom,
            child: Container(
              height: 1.0,
              decoration: const BoxDecoration(
                border: Border(bottom: BorderSide(color: Color(0xFFBDBDBD), width: 0.0))
              ),
            ),
          ),
        ],
      );
    }

转换成这个

if (!DropdownButtonHideUnderline.at(context)) {
      final double bottom = widget.isDense ? 0.0 : 8.0;
      result = Stack(
        children: <Widget>[
          result,
          Positioned(
            left: 0.0,
            right: 0.0,
            bottom: bottom,
            child: Container(
              height: 1.0,
              decoration: const BoxDecoration(
                border: Border(bottom: BorderSide(color: Colors.red

("这里你想要的任何颜色")
, width: 0.0))
                  ),
                ),
              ),
            ],
          );
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接