Making a dropdown menu in Flutter with a Map. Use the below example code to create a dropdown menu in Flutter with a Map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
static const Map<String, Duration> frequencyOptions = { "30 seconds": Duration(seconds: 30), "1 minute": Duration(minutes: 1), "2 minutes": Duration(minutes: 2), }; Duration _frequencyValue = Duration(seconds: 30); @override Widget build(BuildContext context) { return DropdownButton<Duration>( items: frequencyOptions .map((description, value) { return MapEntry( description, DropdownMenuItem<Duration>( value: value, child: Text(description), )); }) .values .toList(), value: _frequencyValue, onChanged: (newValue) { setState(() { _frequencyValue = newValue; }); }, ); } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.