简体   繁体   English

自定义 DropdownButton 的弹出菜单

[英]Customizing the popup menu of a DropdownButton

How can you set the background color of the DropdownButton menu.如何设置DropdownButton菜单的背景颜色。 I can customize the Text() items that appear but they appear within a container which I would like to change the color for.我可以自定义出现的Text()项目,但它们出现在我想更改颜色的容器中。

Looks like dropdownColor setting handles this:看起来dropdownColor设置处理这个:

DropdownButton<String>(
  dropdownColor: Colors.blue,

 // ...
}

在此处输入图片说明

.. found it thanks to options offered by autocomplete .. 通过自动完成提供的选项找到它

Something like this will work:像这样的事情会起作用:

          DropdownMenuItem<int>(
            value: model.id,
            child: SizedBox(
              width: width,
              child: Container(
                color: Colors.green, // 
                child: Text(
                  model.toString(),
                ),
              ),
            ),
          )
        ) 
int _value = 0;

Widget _buildDropdown() {
  return DropdownButton(
    value: _value,
    items: [
      DropdownMenuItem(
        value: 0,
        child: Container(
          color: Colors.blue, // you need this
          child: Text("Zero"),
          width: 100,
          alignment: Alignment.center,
        ),
      ),
      DropdownMenuItem(
        value: 1,
        child: Container(
          color: Colors.green, // you need this
          child: Text("One"),
          width: 100,
          alignment: Alignment.center,
        ),
      ),
    ],
    onChanged: (value) => setState(() => _value = value),
  );
}

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

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