From 8ae24dc6aae9c3d81a464743448b04eb3f6cc8aa Mon Sep 17 00:00:00 2001 From: Nishant Srivastava Date: Thu, 15 Mar 2018 09:55:50 +0530 Subject: [PATCH] implemented: dropdown_button example --- dropdown_button/lib/main.dart | 49 +++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/dropdown_button/lib/main.dart b/dropdown_button/lib/main.dart index 64dc6ba..01b8ab7 100644 --- a/dropdown_button/lib/main.dart +++ b/dropdown_button/lib/main.dart @@ -2,7 +2,40 @@ import 'package:flutter/material.dart'; void main() => runApp(new MyApp()); -class MyApp extends StatelessWidget { +class MyApp extends StatefulWidget { + @override + State createState() { + return new MyAppState(); + } +} + +class MyAppState extends State { + List _fruits = ["Apple", "Banana", "Pineapple", "Mango", "Grapes"]; + + List> _dropDownMenuItems; + String _selectedFruit; + + @override + void initState() { + _dropDownMenuItems = buildAndGetDropDownMenuItems(_fruits); + _selectedFruit = _dropDownMenuItems[0].value; + super.initState(); + } + + List> buildAndGetDropDownMenuItems(List fruits) { + List> 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: [ + new Text("Please choose a fruit: "), + new DropdownButton( + value: _selectedFruit, + items: _dropDownMenuItems, + onChanged: changedDropDownItem, + ) + ], + )), ), ), );