如何更改下拉按钮图标颜色?扑

问题描述 投票:3回答: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;
           });
         })

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

flutter dropdown
7个回答
5
投票

由于DropdownButton从最近的Theme获得颜色,您有两种选择。

第一个是通过改变应用程序主题的亮度。

另一种是用一个新的dropdown和黑暗的Theme包裹你的brightness按钮。

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
投票

这有点像黑客攻击,但是它可以让你完全控制下拉列表的崩溃方式,简而言之,值为:null,提示:null,iconsize:null,使一个堆栈有2个容器具有相同的大小:1显示您的折叠下拉列表和1检测到手势'展开'。

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
投票

目前,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,并根据所选内容动态生成“提示”。这样做可以让您完全控制提示小部件。

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))
                  ),
                ),
              ),
            ],
          );
        }

0
投票

您可以通过以下方式使用字段iconEnabledColoriconDisabledColor

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

0
投票

将您的小部件包裹在具有您想要设置的值的新主题周围,因为您可以转到源代码并查看它使用的主题颜色。

© www.soinside.com 2019 - 2024. All rights reserved.