Last active
April 30, 2021 22:56
-
-
Save alexanderytaylor/3423a6568497750562ef6db12533c814 to your computer and use it in GitHub Desktop.
DropDownMenuItem seems to be constrained to the width of the DropDownButton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
final Color darkBlue = Color(0xFFFFFFF); | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue), | |
debugShowCheckedModeBanner: false, | |
home: Scaffold( | |
body: Center( | |
child: WhereToFindWidget(), | |
), | |
), | |
); | |
} | |
} | |
class WhereToFindWidget extends StatefulWidget { | |
const WhereToFindWidget({Key? key}) | |
: super(key: key); | |
@override | |
_WhereToFindWidgetState createState() => _WhereToFindWidgetState(); | |
} | |
class _WhereToFindWidgetState extends State<WhereToFindWidget> { | |
late DropDownCountry currentCountry; | |
final List<DropDownCountry> _countrys = countryCodes.entries | |
.map<DropDownCountry>((entry) => DropDownCountry( | |
countryCode: entry.key, | |
countryName: entry.value[0], | |
countryEmoji: entry.value[1])) | |
.toList() | |
..sort((a, b) => a.countryName.compareTo(b.countryName)); | |
@override | |
void initState() { | |
super.initState(); | |
currentCountry = _countrys.firstWhere( | |
(element) => element.countryCode == 'US', | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: | |
DropdownButtonHideUnderline( | |
child: DropdownButton<DropDownCountry>( | |
value: currentCountry, | |
isDense: true, | |
dropdownColor: Color(0xFF222C3E), | |
selectedItemBuilder: (BuildContext context) { | |
return _countrys.map<Widget>((dropDownCountry) { | |
return Text( | |
dropDownCountry.countryEmoji, | |
); | |
}).toList(); | |
}, | |
onChanged: (DropDownCountry? newValue) { | |
setState(() { | |
currentCountry = newValue!; | |
}); | |
}, | |
items: _countrys.map((DropDownCountry dropDownCountry) { | |
return DropdownMenuItem<DropDownCountry>( | |
value: dropDownCountry, | |
child: Container( | |
decoration: BoxDecoration(color: Color(0xFF222C3E)), | |
child: Text( | |
dropDownCountry.countryEmoji + | |
' ' + | |
dropDownCountry.countryName, | |
softWrap: false, | |
), | |
), | |
); | |
}).toList(), | |
), | |
), | |
); | |
} | |
} | |
class DropDownCountry { | |
const DropDownCountry({ | |
required this.countryCode, | |
required this.countryName, | |
required this.countryEmoji, | |
}); | |
final String countryCode; | |
final String countryName; | |
final String countryEmoji; | |
} | |
const Map<String, List<String>> countryCodes = { | |
'AR': ['Argentina', '๐ฆ๐ท'], | |
'AT': ['Austria', '๐ฆ๐น'], | |
'AU': ['Australia', '๐ฆ๐บ'], | |
'BE': ['Belgium', '๐ง๐ช'], | |
'BR': ['Brazil', '๐ง๐ท'], | |
'CA': ['Canada', '๐จ๐ฆ'], | |
'CH': ['Switzerland', '๐จ๐ญ'], | |
'CL': ['Chile', '๐จ๐ฑ'], | |
'CO': ['Colombia', '๐จ๐ด'], | |
'CZ': ['Czechia', '๐จ๐ฟ'], | |
'DE': ['Germany', '๐ฉ๐ช'], | |
'DK': ['Denmark', '๐ฉ๐ฐ'], | |
'EC': ['Ecuador', '๐ช๐จ'], | |
'EE': ['Estonia', '๐ช๐ช'], | |
'ES': ['Spain', '๐ช๐ธ'], | |
'FI': ['Finland', '๐ซ๐ฎ'], | |
'FR': ['France', '๐ซ๐ท'], | |
'GB': ['United Kingdom', '๐ฌ๐ง'], | |
'GR': ['Greece', '๐ฌ๐ท'], | |
'HU': ['Hungary', '๐ญ๐บ'], | |
'ID': ['Indonesia', '๐ฎ๐ฉ'], | |
'IE': ['Ireland', '๐ฎ๐ช'], | |
'IN': ['India', '๐ฎ๐ณ'], | |
'IT': ['Italy', '๐ฎ๐น'], | |
'JP': ['Japan', '๐ฏ๐ต'], | |
'KR': ['Korea', '๐ฐ๐ท'], | |
'LT': ['Lithuania', '๐ฑ๐น'], | |
'LV': ['Latvia', '๐ฑ๐ป'], | |
'MX': ['Mexico', '๐ฒ๐ฝ'], | |
'MY': ['Malaysia', '๐ฒ๐พ'], | |
'NL': ['Netherlands', '๐ณ๐ฑ'], | |
'NO': ['Norway', '๐ณ๐ด'], | |
'NZ': ['New Zealand', '๐ณ๐ฟ'], | |
'PE': ['Peru', '๐ต๐ช'], | |
'PH': ['Philippines', '๐ต๐ญ'], | |
'PL': ['Poland', '๐ต๐ฑ'], | |
'PT': ['Portugal', '๐ต๐น'], | |
'RO': ['Romania', '๐ท๐ด'], | |
'RU': ['Russia', '๐ท๐บ'], | |
'SE': ['Sweden', '๐ธ๐ช'], | |
'SG': ['Singapore', '๐ธ๐ฌ'], | |
'TH': ['Thailand', '๐น๐ญ'], | |
'TU': ['Turkey', '๐น๐ท'], | |
'US': ['United States', '๐บ๐ธ'], | |
'VE': ['Venezuela', '๐ป๐ช'], | |
'ZA': ['South Africa', '๐ฟ๐ฆ'] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment