简体   繁体   中英

Customizing the popup menu of a DropdownButton

How can you set the background color of the DropdownButton menu. I can customize the Text() items that appear but they appear within a container which I would like to change the color for.

Looks like dropdownColor setting handles this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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