xxxxxxxxxx
import 'package:flutter/material.dart';
void main() {
runApp(Example());
}
class Example extends StatefulWidget {
@override
State<StatefulWidget> createState() => _ExampleState();
}
class _ExampleState extends State<Example> {
// List<String> _locations = ['Please choose a location', 'A', 'B', 'C', 'D']; // Option 1
// String _selectedLocation = 'Please choose a location'; // Option 1
List<String> _locations = ['A', 'B', 'C', 'D']; // Option 2
String _selectedLocation; // Option 2
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: DropdownButton(
hint: Text('Please choose a location'), // Not necessary for Option 1
value: _selectedLocation,
onChanged: (newValue) {
setState(() {
_selectedLocation = newValue;
});
},
items: _locations.map((location) {
return DropdownMenuItem(
child: new Text(location),
value: location,
);
}).toList(),
),
),
),
);
}
}
xxxxxxxxxx
DropdownButton(
value: "Tokyo",
items: [
DropdownMenuItem(
child: Text("New York"),
value: "New York"
),
DropdownMenuItem(
child: Text("Tokyo"),
value: "Tokyo",
)
],
)
xxxxxxxxxx
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String dropdownvalue = 'Apple';
var items = ['Apple','Banana','Grapes','Orange','watermelon','Pineapple'];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("DropDownList Example"),
),
body: Container(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("DropDownButton"),
Container(
height: 40,
padding: EdgeInsets.all(5.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30.0),
border: Border.all(
color: Colors.grey, style: BorderStyle.solid, width: 0.80),
),
child: DropdownButtonHideUnderline(
child: DropdownButton(
elevation: 0,
value: dropdownvalue,
icon: Icon(Icons.keyboard_arrow_down),
items:items.map((String items) {
return DropdownMenuItem(
value: items,
child: Text(items)
);
}
).toList(),
onChanged: (String? newValue){
setState(() {
dropdownvalue = newValue!;
});
},
),
),
),
],
),
],
),
),
);
}
}
xxxxxxxxxx
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
title: "Tutorial",
home: Home(),
));
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
int _value = 1;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Dropdown Button"),
),
body: Container(
padding: EdgeInsets.all(20.0),
child: DropdownButton(
value: _value,
items: [
DropdownMenuItem(
child: Text("First Item"),
value: 1,
),
DropdownMenuItem(
child: Text("Second Item"),
value: 2,
),
DropdownMenuItem(
child: Text("Third Item"),
value: 3
),
DropdownMenuItem(
child: Text("Fourth Item"),
value: 4
)
],
onChanged: (value) {
setState(() {
_value = value;
});
}),
));
}
}
xxxxxxxxxx
Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
DropdownButton(
value: currentItem,
items: items
.map<DropdownMenuItem<String>>(
(e) => DropdownMenuItem(
value: e,
child: Text(e),
),
)
.toList(),
onChanged: (String? value) => setState(
() {
if (value != null) currentItem = value;
},
),
),
Text(currentItem),
],
),
)
xxxxxxxxxx
DropdownButton(
value: currentItem,
items: items
.map<DropdownMenuItem<String>>(
(e) => DropdownMenuItem(
value: e,
child: Text(e),
),
)
.toList(),
onChanged: (String? value) => setState(
() {
if (value != null) currentItem = value;
},
),
),
xxxxxxxxxx
List<DropdownMenuItem<String>> getDropdownItems() {
List<DropdownMenuItem<String>> dropDownItems = [];
for (String currency in currenciesList) {
var newDropdown = DropdownMenuItem(
child: Text(currency),
value: currency,
);
dropDownItems.add(newDropdown);
}
return dropDownItems;
}
xxxxxxxxxx
DropdownButton in flutter with initial value, hintText, right/end icon, border and more
>>>>Initialize the following in your state class
List<String> dropDownMenuItem = ["Option 1", "Option2"];
String? selectedValue;
>>>>Your main DropdownButton
Container(
margin: getMargin(top: 16),
child: SizedBox(
width: double.infinity,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25),
border:
Border.all(color: appTheme.gray30003)),
child: DropdownButton<String>(
isExpanded: true,
hint: Text(
"Select an option",
style: theme.textTheme.bodySmall!.copyWith(
color: appTheme.black900,
fontSize: getFontSize(
13,
),
),
),
style: TextThemeHelper.bodySmallBluegray40012,
icon: const Icon(Icons.expand_more_outlined),
padding: EdgeInsets.symmetric(
vertical: 1, horizontal: 20),
underline: SizedBox.shrink(),
value: selectedValue,
items: dropDownMenuItem
.map((String e) => DropdownMenuItem(
value: e, child: Text(e)))
.toList(),
onChanged: (String? value) {
setState(() {
selectedValue = value!;
});
}),
),
),
),