简体   繁体   English

Flutter:如何为 DropdownItems 和 DropdownButton 选定项设置不同的 colors?

[英]Flutter: How to set different colors for DropdownItems and for DropdownButton selected item?

I have included DropdownButton in my project but I stucked with these problem.我已经在我的项目中包含了DropdownButton ,但我遇到了这些问题。

I have tried to use Theme on it but it also changes the both of colors.我曾尝试在其上使用主题,但它也同时更改了 colors。 I can still change the background color of dropdown but I wanted it to be white with black text.我仍然可以更改下拉菜单的背景颜色,但我希望它是带有黑色文本的白色。

Here you can see the screens , the dropdown is white because text color is also white在这里你可以看到屏幕,下拉菜单是白色的,因为文本颜色也是白色的

AccentColorOverride(
  child: Theme(
    data: ThemeData(
        hintColor: Colors.white,
        selectedRowColor: Colors.white),
    child: DropdownButton<String>(
      value: selectedRegion,
      hint: Text(hint_label_region, style: white18),
      isExpanded: true,
      underline: Container(
        height: 1.0,
        decoration: const BoxDecoration(
            border: Border(
                bottom: BorderSide(
                    color: Color(0xFFBDBDBD),
                    width: 2))),
      ),
      items: <String>[
        'A',
        'B',
        'C',
        'D'
      ].map<DropdownMenuItem<String>>((String value) {
        return DropdownMenuItem<String>(
          value: value,
          child: new Text(
            value,
            style: TextStyle(color: Colors.white),
          ),
        );
      }).toList(),
      onChanged: (String newValue) {
        setState(() {
          selectedRegion = newValue;
        });
      },
    ),
  ),
)

If anyone still needs, by Flutter docs you can set a custom selectedItemBuilder, which means you can basically display anything you want respecting the List type.如果有人仍然需要,通过Flutter 文档,您可以设置自定义 selectedItemBuilder,这意味着您基本上可以显示任何您想要尊重列表类型的内容。 Here's an example:这是一个例子:

var listSorted = list.map((dropdownItem) {
    return DropdownMenuItem<dynamic>(
        child: Text("${dropdownItem.label}",
            style: TextStyle(color: Colors.black)),
        value: dropdownItem.value);
    }).toList();
return Container(
      child: DropdownButton(
        isExpanded: true,
        iconEnabledColor: Colors.white,
        underline: Container(
          width: 200,
          height: 1,
          color: Colors.white,
        ),
        value: valueToUpdate,
        items: listSorted,
        onChanged: onChanged,
        selectedItemBuilder: (BuildContext ctxt) {
          return list.map<Widget>((item) {
            return DropdownMenuItem(
                child: Text("${item.label}",
                    style: TextStyle(color: Colors.white)),
                value: item.value);
          }).toList();
        },
        dropdownColor: backgroundColor,
      )));

I think this should work, define a state like '_selectedItemValue', call setState when user taps on an item, and render your DropDownMenuItem based on this state.我认为这应该可行,定义一个 state 像'_selectedItemValue',当用户点击一个项目时调用 setState,并根据这个 state 呈现你的 DropDownMenuItem。

    .
    .
    .
    ].map<DropdownMenuItem<String>>((String value) {
            return GestureDetector(
              onTap: () {
                 setState() {
                   _selectedItemValue = value;
                 }
              } ,
              child:DropdownMenuItem<String>(
                value: value,
                child: Container(
                  color: value == _selectedItemValue ? Colors.blue : Colors.white,
                  child: new Text(
                    value,
                    style: TextStyle(color: Colors.white),
                ),),
            ),);
          }).toList(),

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

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