The default DropdownButton with DropdownMenuItems returns a light-grey dropdown. How should I customize the dropdown (e.g. background color, dropdown width)? I can change the style
property in both DropdownButton and DropdownMenuItem, like this:
return new DropdownButton(
value: ...,
items: ...,
onChanged: ...,
style: new TextStyle(
color: Colors.white,
),
);
but this doesn't change the dropdown's background color.
Should I copy DropdownMenu and extend it? Does Flutter plan to add customization for this widget in the near future?
I was able to change the background for the Dropdown by wrapping it in a
Container
with thecolor
property set.Before:
After:
Here's the code:
You can accomplish this by wrapping the
DropdownButton
in aTheme
widget and overriding thecanvasColor
.As Collin said, your
DropdownMenuItem
will follow yourThemeData
class. Not only itsbackgroundColor
will match thecanvasColor
in yourThemeData
class, but also it will follow the sameTextStyle
.So, for a quick example:
Furthermore, if you want to control the
width
of the menu, you can feed itschild
property a newContainer
and add the desiredwidth
, check the following GIF, I started withwidth: 100.0
then hot reloaded after changing it to200.0
, notice how thewidth
was manipulated, just make sure you use a suitable width so that you do not get overflow problems later on when you use the menu within a more complex layout.