Skip to content

Instantly share code, notes, and snippets.

@alexanderytaylor
Last active April 30, 2021 22:56
Show Gist options
  • Save alexanderytaylor/3423a6568497750562ef6db12533c814 to your computer and use it in GitHub Desktop.
Save alexanderytaylor/3423a6568497750562ef6db12533c814 to your computer and use it in GitHub Desktop.
DropDownMenuItem seems to be constrained to the width of the DropDownButton
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