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

2 Answers 2

2

you should rearrange your list after selecting an item

onChanged: (value) {
  dropdownvalue.value = value.toString();
  items.remove("Time");
  items.insert(0, "Time");
  setState((){});
},
Sign up to request clarification or add additional context in comments.

2 Comments

this does the trick, it could get confusing cause the order is different every time you pick a different option
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
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

3 Comments

It didn't do the trick unfortunately, I added images so you can see the problem.
i have changed the answer can you try with initialise e value like this
did you change anything? I didn't notice changes, tried again but the same problem emerges

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.