skip to Main Content

How to not show the selected item in the dropdown?
I m using DropdownButtonHideUnderline package in flutter to implement a dropdown but i don’t want to show the selected item on the top.And i just need the selected item in a variable .How to do this?

Please help..

DropdownButtonHideUnderline(
    child: DropdownButton2(
  // Initial Value
  value: dropdownValue,

  // Down Arrow Icon
  icon: const Icon(
    Icons.more_vert,
    color: Colors.white,
  ),

  // Array list of items
  items: items.map((String items) {
    return DropdownMenuItem(
      value: items,
      child: Text(
        items,
        style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
      ),
    );
  }).toList(),
  // After selecting the desired option,it will
  // change button value to selected value
  onChanged: (String? newValue) {
    setState(() {
      dropdownValue = newValue!;
    });
  },
))

2

Answers


  1. here you change the selected value:

     onChanged: (String? newValue) {
                                      setState(() {
                                        dropdownValue = newValue!;
                                      });
    }
    

    by removing

    setState(() {
                                        dropdownValue = newValue!;
                                      });
    

    your dropdown will not change.

    and you can use the chosen value by

    onChanged: (String? newValue) => doSomething(newValue),
    
    Future<void> doSomething(String? value) async {
    ...
    }
    
    Login or Signup to reply.
  2. You can ignore setting value on onChanged,

    here, remove this, this is used to update the value on dropdownButton
    .

    onChanged: (String? newValue) {
      // setState(() {
      //   dropdownValue = newValue!;
      // });
    },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search