0

How can I make the selected dropdown item appear first in the list when its opened? I'm using GetX for state,

is this the proper way of doing it anyway?

This is the code:

var dropdownvalue = 'Default'.obs;
  var items = [
    'Default',
    'Difficulty',
    'Time',
    'Distance',
  ];
  RxInt selectedIndex = 0.obs;

...

Obx(() => DropdownButton(
                    value: dropdownvalue.value,
                    items: items.map((String items) {
                      return DropdownMenuItem(
                        value: items,
                        child: Text(items),
                      );
                    }).toList(),
                    onChanged: (value) {
                      dropdownvalue.value = value.toString();
                    },
                  )

This is how it looks now:

enter image description here

The list should stay in one place when its opened, and values should change places.

Edit: now it shows the same value

enter image description here

enter image description here

Gryva
  • 297
  • 1
  • 11

2 Answers2

2

you should rearrange your list after selecting an item

onChanged: (value) {
  dropdownvalue.value = value.toString();
  items.remove("Time");
  items.insert(0, "Time");
  setState((){});
},
Ahmed Elhenidy
  • 219
  • 1
  • 8
  • this does the trick, it could get confusing cause the order is different every time you pick a different option – Gryva Sep 19 '22 at 11:39
  • 1
    yeah, but you can make a temp list with the original items and every time you select an item from the drop down you put that item in the first and assign this temp list to the dropdown list – Ahmed Elhenidy Sep 20 '22 at 08:18
0
var selected value = "";

initState(){
value = dropdownvalue.value;
}

    Obx(() => DropdownButton(
                        value: dropdownvalue.value,
                        items: items.map((String items) {
                          return DropdownMenuItem(
                            value: items,
                            child: Text( value), //changes here 
                          );
                        }).toList(),
                        onChanged: (value) {
                          dropdownvalue.value = value.toString();
                        },
                      )


Change there your problem will be solved