Flutter:如何为DropdownItems和DropdownButton所选项目设置不同的颜色?

问题描述 投票:0回答:1

我已经在项目中加入了DropdownButton,但仍然遇到了这些问题。

我曾尝试在其上使用主题,但它同时更改了两种颜色。我仍然可以更改下拉菜单的背景颜色,但我希望它是白色和黑色文本。

在这里您可以看到screens,下拉菜单为白色,因为文本颜色也为白色

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;
        });
      },
    ),
  ),
)
flutter drop-down-menu dropdown
1个回答
0
投票

我认为这应该可行,定义一个类似于'_selectedItemValue'的状态,当用户点击一个项目时调用setState,并基于此状态呈现您的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(),
© www.soinside.com 2019 - 2024. All rights reserved.