Created
April 29, 2020 08:31
-
-
Save vignarajj/c8992ad792a67e4e86be7d2324ff7653 to your computer and use it in GitHub Desktop.
Simple drop down example in flutter
This file contains 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/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
void main() => runApp(DropDownExample()); | |
class DropDownExample extends StatefulWidget { | |
DropDownExample() : super(); | |
final String title = "DropDown Demo"; | |
@override | |
DropDownExampleState createState() => DropDownExampleState(); | |
} | |
class Country { | |
int id; | |
String name; | |
Country(this.id, this.name); | |
static List<Country> getCountries() { | |
return <Country>[ | |
Country(1, 'India'), | |
Country(2, 'Malaysia'), | |
Country(3, 'UAE'), | |
Country(4, 'Indonesia'), | |
Country(5, 'Nepal'), | |
]; | |
} | |
} | |
class DropDownExampleState extends State<DropDownExample> { | |
// | |
List<Country> _countries = Country.getCountries(); | |
List<DropdownMenuItem<Country>> _dropdownMenuItems; | |
Country _selectedCountry; | |
@override | |
void initState() { | |
_dropdownMenuItems = buildDropdownMenuItems(_countries); | |
_selectedCountry = _dropdownMenuItems[0].value; | |
super.initState(); | |
} | |
List<DropdownMenuItem<Country>> buildDropdownMenuItems(List countries) { | |
List<DropdownMenuItem<Country>> items = List(); | |
for (Country company in countries) { | |
items.add( | |
DropdownMenuItem( | |
value: company, | |
child: Text( | |
company.name, | |
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18), | |
), | |
), | |
); | |
} | |
return items; | |
} | |
onChangeDropdownItem(Country selectedCompany) { | |
setState(() { | |
_selectedCountry = selectedCompany; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
debugShowCheckedModeBanner: false, | |
home: new Scaffold( | |
appBar: new AppBar( | |
title: new Text("DropDown Example"), | |
), | |
body: new Container( | |
child: Center( | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.center, | |
mainAxisAlignment: MainAxisAlignment.start, | |
children: <Widget>[ | |
Align( | |
child: Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: Text( | |
"Select a Country", | |
style: | |
TextStyle(fontWeight: FontWeight.bold, fontSize: 18), | |
), | |
), | |
alignment: Alignment.topLeft, | |
), | |
SizedBox( | |
height: 20.0, | |
), | |
Padding( | |
padding: const EdgeInsets.all(8.0), | |
child: DropdownButton( | |
value: _selectedCountry, | |
items: _dropdownMenuItems, | |
isExpanded: true, | |
underline: SizedBox(), | |
onChanged: onChangeDropdownItem, | |
/* hint: Text( | |
"Select a Country", | |
style: | |
TextStyle(fontWeight: FontWeight.bold, fontSize: 18), | |
),*/ // you can set hint like that. | |
), | |
), | |
SizedBox( | |
height: 20.0, | |
), | |
Text( | |
'Selected: ${_selectedCountry.name}', | |
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 15), | |
), | |
], | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
like