@@ -22,42 +22,79 @@ class MyHomePage extends StatefulWidget {
22
22
}
23
23
24
24
class _MyHomePageState extends State <MyHomePage > {
25
+ List <String > added = [];
26
+ String currentText = "" ;
27
+ GlobalKey <AutoCompleteTextFieldState <String >> key = new GlobalKey ();
28
+
25
29
@override
26
30
Widget build (BuildContext context) {
31
+ var textField = new AutoCompleteTextField <String >(
32
+ key: key,
33
+ suggestions: [
34
+ "Apple" ,
35
+ "Armidillo" ,
36
+ "Actual" ,
37
+ "Actuary" ,
38
+ "America" ,
39
+ "Argentina" ,
40
+ "Australia" ,
41
+ "Antarctica" ,
42
+ "Blueberry" ,
43
+ "Cheese" ,
44
+ "Danish" ,
45
+ "Eclair" ,
46
+ "Fudge" ,
47
+ "Granola" ,
48
+ "Hazelnut" ,
49
+ "Ice Cream" ,
50
+ "Jely" ,
51
+ "Kiwi Fruit" ,
52
+ "Lamb" ,
53
+ "Macadamia" ,
54
+ "Nachos" ,
55
+ "Oatmeal" ,
56
+ "Palm Oil" ,
57
+ "Quail" ,
58
+ "Rabbit" ,
59
+ "Salad" ,
60
+ "T-Bone Steak" ,
61
+ "Urid Dal" ,
62
+ "Vanilla" ,
63
+ "Waffles" ,
64
+ "Yam" ,
65
+ "Zest"
66
+ ],
67
+ textChanged: (item) {
68
+ currentText = item;
69
+ },
70
+ itemBuilder: (context, item) {
71
+ return new Padding (
72
+ padding: EdgeInsets .all (8.0 ), child: new Text (item));
73
+ },
74
+ itemSorter: (a, b) {
75
+ return a.compareTo (b);
76
+ },
77
+ itemFilter: (item, query) {
78
+ return item.toLowerCase ().startsWith (query.toLowerCase ());
79
+ });
80
+
27
81
Column body = new Column (children: [
28
82
new ListTile (
29
- title: new AutoCompleteTextField <String >(
30
- suggestions: [
31
- "Apple" ,
32
- "Blueberry" ,
33
- "Cheese" ,
34
- "Danish" ,
35
- "Eclair" ,
36
- "Fudge" ,
37
- "Granola"
38
- ],
39
- textChanged: (item) {
40
-
41
- },
42
- itemBuilder: (context, item) {
43
- return new Padding (
44
- padding: EdgeInsets .all (8.0 ),
45
- child: new Text (item)
46
- );
47
- },
48
- itemSorter: (a, b) {
49
- return a.compareTo (b);
50
- },
51
- itemFilter: (item, query) {
52
- return item.toLowerCase ().startsWith (query.toLowerCase ());
53
- }),
54
- trailing: new IconButton (icon: new Icon (Icons .add), onPressed: () {
55
-
56
- })
57
-
58
- )
83
+ title: textField,
84
+ trailing: new IconButton (
85
+ icon: new Icon (Icons .add),
86
+ onPressed: () {
87
+ setState (() {
88
+ added.add (currentText);
89
+ textField.clear ();
90
+ });
91
+ }))
59
92
]);
60
93
94
+ body.children.addAll (added.map ((item) {
95
+ return new ListTile (title: new Text (item));
96
+ }));
97
+
61
98
return new Scaffold (
62
99
appBar: new AppBar (title: new Text ('Auto Complete TextField Demo' )),
63
100
body: body);
0 commit comments