skip to Main Content

How to change the selected item background color in the opened DropdownMenu? I want to change the selected item background color in the opened DropdownMenu.

2

Answers


  1. Chosen as BEST ANSWER

    I can change color by adding MenuStyle

    DropdownMenu(
      menuStyle: MenuStyle(
        backgroundColor:  MaterialStateProperty.all( Colors.grey),
      ),
    ),
    

  2. In Flutter, you can change the selected item’s background color in an opened DropdownButton by customizing the items and using a DropdownMenuItem or with a custom background color. Here’s how you can do it:

    1. Create a list of DropdownMenuItem or widgets and customize their appearance:
         DropdownButton<String>(
             value: selectedValue,
             items: items.map((String item) {
                 return DropdownMenuItem<String>(
                     value: item,
                     child: Container(
                         color: item == selectedValue ? Colors.blue : Colors.white, // Change the background color for the selected item
                         child: Text(item),
                     ),
                 );
             }).toList(),
             onChanged: (String newValue) {
                 setState(() {
                     selectedValue = newValue;
                 });
             },
         )
    

    In the code above, the Container’s background color is determined by the comparison of the current item with the selected value. If it matches, it sets the background color to blue (you can change it to any color you like), and for other items, it sets the background color to white. You can customize the colors to your preference.

    • Ensure that you have a selectedValue variable and update it in the onChanged callback to reflect the selected value.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search