mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2025-05-17 21:15:54 +08:00
implemented: dropdown_button example
This commit is contained in:
@ -2,7 +2,40 @@ import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(new MyApp());
|
||||
|
||||
class MyApp extends StatelessWidget {
|
||||
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(
|
||||
@ -11,7 +44,19 @@ class MyApp extends StatelessWidget {
|
||||
title: new Text("DropDown Button Example"),
|
||||
),
|
||||
body: new Container(
|
||||
child: new Center(child: new Text("Hello World")),
|
||||
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,
|
||||
)
|
||||
],
|
||||
)),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
Reference in New Issue
Block a user