This is my list view which is e.g
List<String>items = <String>[
'Leave Type',
'CL',
'SL',
'EL', ];
String dropdownValue = 'Leave Type';
as i Created dropdown here the widget below sample
Padding(
padding:EdgeInsets.all(10.0),
child:
DropdownButton<String>(
iconSize: 24,
elevation: 16,
onChanged:(String? newValue){
setState((){
dropdownValue = newValue!;
});
},
value:dropdownValue,
items:items.map<DropdownMenuItem<String>>(
(String value){
return DropdownMenuItem<String>(
value: value,
child:Text(value),
);
},
).toList(),
)
),
Now want to pass the value into database
Related
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!;
});
},
),
),
);
}
}
I got issue to display value after select option... It always display Type All. I've follow from this answer but still got the problem.
DropdownButton(
hint: Text('Filter All Type'),
value: _typeDataL,
onChanged: (_newValue) {
setState(() {
_typeDataL = _newValue;
// print(typeDataL);
});
print(_newValue);
},
items: typeDataL.map((value) {
return DropdownMenuItem(
value: value,
child: new Text(value),
);
}).toList(),
),
try below code,
String? selectedValue = "Your init value";
var typeDataL = ["Your init value", "Option1", "Option2", "Option3"];
DropdownButton(
hint: const Text('Filter All Type'),
value: selectedValue,
onChanged: (newValue) {
setState(() {
selectedValue = newValue;
});
},
items: typeDataL.map((value) {
return DropdownMenuItem(
value: value,
child: Text(value),
);
}).toList(),
),
Your valueList should contain initial value, same as initial selectedValue and valueList should not contain duplicate values.
My dropDown working fine, instead of Today as a string in the dropDown initial value I want Today's date-time using the property DateTime.now()
String dropdownvalue = 'Today';
var items = [
'Today',
'Choose from calendar',
];
My dropdown:
DropdownButton(
underline: Container(),
isExpanded: true,
value: dropdownvalue,
icon: const Icon(
Icons.keyboard_arrow_down,
color: Color(0xffB50000),
),
items: items.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(
items,
style: TextStyle(
color: Color(0xffB50000),
fontWeight: FontWeight.w400),
),
);
}).toList(),
onChanged: (String? newValue) {
setState(() {
dropdownvalue = newValue!;
});
if(dropdownvalue =='Choose from calendar'){
setState(() {
_selectedDate(context);
});
}
if(dropdownvalue =='Today'){
setState(() {
String today = DateFormat('dd-MM-yyyy').format(currentDateTime);
print("today");
print(today);
final storeProvider = Provider.of<StorageProvider>(context, listen: false);
storeProvider.updateAppointmentDate(today);
final slotBookingProvider = Provider.of<SlotBookingProvider>(context, listen: false);
slotBookingProvider.checkAvailableSlot(date: today, context: context);
});
}
},
),
You can
You can replace replacing Today with DateTime.now().toString()
String? dropdownvalue;
var items = [
DateTime.now().toString(),
'Choose from calendar',
];
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 change Dropdown arrow color?
Here is what I want:
This is what I get
My widget:
DropdownButtonHideUnderline (
child: DropdownButton<String>(
isExpanded: true,
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['Bank Deposit', 'Mobile Payment', 'Cash Pickup']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
})
.toList(),
),
),
I tried wrapping with Theme and changing Brightness, but it changes arrow from White to Black only. I want to use some other color.
This can be done with icon: property in DropdownButton
DropdownButtonHideUnderline(
child: DropdownButton<String>(
isExpanded: true,
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
hint: Text('Select'),
icon: Icon( // Add this
Icons.arrow_drop_down, // Add this
color: Colors.blue, // Add this
),
items: <String>['Bank Deposit', 'Mobile Payment', 'Cash Pickup']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
),
),
Thanks to #anmol.majhail, anyway found something simpler using iconEnabledColor property.
DropdownButtonHideUnderline (
child: DropdownButton<String>(
iconEnabledColor: Colors.indigo, // game changer
isExpanded: true,
value: dropdownValue,
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['Bank Deposit', 'Mobile Payment', 'Cash Pickup']
.map<DropdownMenuItem<String>>((String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
})
.toList(),
),
),