mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2025-05-17 13:05:53 +08:00
66 lines
1.7 KiB
Dart
66 lines
1.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
void main() => runApp(new MyApp());
|
|
|
|
class MyApp extends StatefulWidget {
|
|
@override
|
|
State<StatefulWidget> createState() {
|
|
return new MyAppState();
|
|
}
|
|
}
|
|
|
|
class MyAppState extends State<MyApp> {
|
|
List _fruits = ["Apple", "Banana", "Pineapple", "Mango", "Grapes"];
|
|
|
|
List<DropdownMenuItem<String>> _dropDownMenuItems;
|
|
String _selectedFruit;
|
|
|
|
@override
|
|
void initState() {
|
|
_dropDownMenuItems = buildAndGetDropDownMenuItems(_fruits);
|
|
_selectedFruit = _dropDownMenuItems[0].value;
|
|
super.initState();
|
|
}
|
|
|
|
List<DropdownMenuItem<String>> buildAndGetDropDownMenuItems(List fruits) {
|
|
List<DropdownMenuItem<String>> items = new List();
|
|
for (String fruit in fruits) {
|
|
items.add(new DropdownMenuItem(value: fruit, child: new Text(fruit)));
|
|
}
|
|
return items;
|
|
}
|
|
|
|
void changedDropDownItem(String selectedFruit) {
|
|
setState(() {
|
|
_selectedFruit = selectedFruit;
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return new MaterialApp(
|
|
debugShowCheckedModeBanner: false,
|
|
home: new Scaffold(
|
|
appBar: new AppBar(
|
|
title: new Text("DropDown Button Example"),
|
|
),
|
|
body: new Container(
|
|
child: new Center(
|
|
child: new Column(
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: <Widget>[
|
|
new Text("Please choose a fruit: "),
|
|
new DropdownButton(
|
|
value: _selectedFruit,
|
|
items: _dropDownMenuItems,
|
|
onChanged: changedDropDownItem,
|
|
)
|
|
],
|
|
)),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|