How to not show the selected dropdown item in flutter - flutter

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!;
});
},
))

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 {
...
}

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!;
// });
},

Related

How to Create a Single Selected Dropdown List in Flutter?

I am a flutter beginner. How to Create a Single Selected Dropdown List in Flutter?
Like This.
I tried, but I didn't get what I wanted.
String dropdownvalue = 'Bahrain';
Container(
width: 308,
child: DropdownButton(
// Initial Value
value: dropdownvalue,
// Down Arrow Icon
icon: const Icon(Icons.keyboard_arrow_down),
// Array list of items
items: dropdownvalue.map((String dropdownvalue) {
return DropdownMenuItem(
value: dropdownvalue,
child: Text(dropdownvalue),
);
}).toList(),
// After selecting the desired option,it will
// change button value to selected value
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
},
),
),
You should have list of items and selectedItem to manage the list and selection state.
class _YourState extends State<MyHomePage> {
List<String> countries = [
'Bahrain',
'India',
'Iraq',
'America',
];
String? dropdownvalue ;
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: DropdownButton(
// Initial Value
value: dropdownvalue,
icon: const Icon(Icons.keyboard_arrow_down),
isExpanded: true,
items: countries.map((String dropdownvalue) {
return DropdownMenuItem(
value: dropdownvalue,
child: Text(dropdownvalue),
);
}).toList(),
hint: Text('Country'),
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
},
),
),
);
}
}

Flutter firebase drowpdown items cannot display

I try to make a dropdown taking data from firestore, after selecting the items from DromdownMenuItem the selected new value cannot display please help..?
DropdownButtonHideUnderline(
child: DropdownButton<String>(
isDense: true,
//isExpanded: false,
value: projectName,
onChanged: (String? newValue) {
debugPrint('selected onchange: $newValue');
setState(
() {
debugPrint('make selected: $newValue');
projectName = newValue.toString();
state.didChange(newValue);
setDefaultMake = false;
///var setDefaultMakeModel = true;
controllerProjectName.text = newValue.toString();
},
);
},
items: snapshot.data!.docs.map((value) {
return DropdownMenuItem<String>(
value: value.get('project_name'),
child: Text('${value.get('project_name')}'),
);
}).toList(),
),
),

Why isn't my DropdownButtonFormField showing the items?

I have this Dropdown:
DropdownButtonFormField(
value: shipmentSelected,
hint: Text(
'choose one',
),
onChanged: (value){
shipmentSelected = value;
}
items: product.shipment.map((Shipment shipment) {
return DropdownMenuItem(
value: shipment.code,
child: Text(
shipment.code,
),
);
}).toList(),
)
But it doesn't show any item... just show the hintText. How to fix it?
You need to pass a callback function to onChanged in the DropdownButtonFormField constructor. Update shipmentSelected and rebuild from this function like so:
onChanged: (value) {
setState(() {
shipmentSelected = value;
});
},

Dropdown button selection not update in Flutter

I created my dropdown button, but whenever an item is selected does not update value.But whenever I hotreload all my values ​​are updated. Can anyone guess the reason? or is there a solution?
DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.keyboard_arrow_down),
iconSize: 15,
elevation: 10,
style: TextStyle(color: Colors.grey),
onChanged: (newValue) {
setState(() async {
dropdownValue = newValue;
final selectedLanguage= await SharedPreferences.getInstance();
selectedLanguage.setString('selectedLanguage', dropdownValue);
});
allTranslations.setNewLanguage(
allTranslations.getLocaleKey(
dropdownValue,
),
);
},
items: <String>[
englishText,
chineseText,
russianText,
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
#kimSoo great that it worked!
Solution for everyone is the async closure in setstate wont work. We have to remove it and initialize Shared Preferences earlier.
Initialize final selectedLanguage= await SharedPreferences.getInstance(); earlier
DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.keyboard_arrow_down),
iconSize: 15,
elevation: 10,
style: TextStyle(color: Colors.grey),
onChanged: (newValue) {
setState(() { <----- removed async
dropdownValue = newValue;
selectedLanguage.setString('selectedLanguage', dropdownValue);
});
allTranslations.setNewLanguage(
allTranslations.getLocaleKey(
dropdownValue,
),
);
},
items: <String>[
englishText,
chineseText,
russianText,
].map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),

How to trigger the DropDownMenu on navigating to a page?

I want my dropDownButton to show the DropDownmenu for selection as soon as the user navigates to the page(instead of manually having to click the button). How can I achieve this ?
My DropDown button
DropdownButton<String>(
isDense: true,
isExpanded: true,
value: _currentlySelected,
onChanged: (String newValue) {
setState(() {
_currentlySelected = newValue;
});
},
items: items.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),