Skip to content

Commit 5de3f17

Browse files
committed
Added a working example for TypeAhead usage
1 parent edfc04a commit 5de3f17

File tree

1 file changed

+58
-67
lines changed

1 file changed

+58
-67
lines changed

example/lib/main.dart

Lines changed: 58 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,6 @@ class MyApp extends StatelessWidget {
1616
}
1717
}
1818

19-
class AppProfile {
20-
final String name;
21-
final String email;
22-
final String imageUrl;
23-
24-
const AppProfile(this.name, this.email, this.imageUrl);
25-
26-
@override
27-
bool operator ==(Object other) =>
28-
identical(this, other) ||
29-
other is AppProfile &&
30-
runtimeType == other.runtimeType &&
31-
name == other.name;
32-
33-
@override
34-
int get hashCode => name.hashCode;
35-
36-
@override
37-
String toString() {
38-
return name;
39-
}
40-
}
41-
4219
class MyHomePage extends StatelessWidget {
4320
@override
4421
Widget build(BuildContext context) {
@@ -61,6 +38,9 @@ class MyHomePage extends StatelessWidget {
6138
'https://upload.wikimedia.org/wikipedia/commons/7/7c/Profile_avatar_placeholder_large.png'),
6239
];
6340

41+
const allCountries = ["Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "France Metropolitan", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard and Mc Donald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao, People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia, The Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia (Slovak Republic)", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbard and Jan Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"];
42+
43+
6444
return Scaffold(
6545
appBar: AppBar(
6646
title: Text('Flutter FormBuilder Example'),
@@ -77,8 +57,10 @@ class MyHomePage extends StatelessWidget {
7757
label: 'Chips',
7858
attribute: 'chips_test',
7959
require: true,
80-
value: [AppProfile('Stock Man', '[email protected]',
81-
'https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX4057996.jpg')],
60+
value: [
61+
AppProfile('Stock Man', '[email protected]',
62+
'https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX4057996.jpg')
63+
],
8264
suggestionsCallback: (String query) {
8365
if (query.length != 0) {
8466
return mockResults.where((profile) {
@@ -102,7 +84,7 @@ class MyHomePage extends StatelessWidget {
10284
},
10385
suggestionBuilder: (context, state, profile) {
10486
return ListTile(
105-
key: ObjectKey(profile),
87+
// key: ObjectKey(profile),
10688
leading: CircleAvatar(
10789
backgroundImage: NetworkImage(profile.imageUrl),
10890
),
@@ -112,6 +94,25 @@ class MyHomePage extends StatelessWidget {
11294
);
11395
},
11496
),
97+
FormBuilderInput.typeAhead(
98+
label: 'Country',
99+
attribute: 'country',
100+
require: true,
101+
itemBuilder: (context, country) {
102+
return ListTile(
103+
title: Text(country),
104+
);
105+
},
106+
suggestionsCallback: (query) {
107+
if (query.length != 0) {
108+
return allCountries.where((country) {
109+
return country.toLowerCase().contains(query.toLowerCase());
110+
}).toList(growable: false);
111+
} else {
112+
return allCountries;
113+
}
114+
},
115+
),
115116
FormBuilderInput.textField(
116117
type: FormBuilderInput.TYPE_TEXT,
117118
attribute: "name",
@@ -186,15 +187,11 @@ class MyHomePage extends StatelessWidget {
186187
label: "My Best Language",
187188
attribute: "best_language",
188189
require: true,
189-
options: [
190-
FormBuilderInputOption(value: "Dart"),
191-
FormBuilderInputOption(value: "Kotlin"),
192-
FormBuilderInputOption(value: "Java"),
193-
FormBuilderInputOption(value: "Swift"),
194-
FormBuilderInputOption(value: "Objective-C"),
195-
],
190+
options: ["Dart", "Kotlin", "Java", "Swift", "Objective-C"]
191+
.map((lang) => FormBuilderInputOption(value: lang))
192+
.toList(growable: false),
196193
),
197-
/*FormBuilderInput.checkbox(
194+
FormBuilderInput.checkbox(
198195
label: "I accept the terms and conditions",
199196
attribute: "accept_terms",
200197
hint:
@@ -239,39 +236,10 @@ class MyHomePage extends StatelessWidget {
239236
label: "Movie Rating (Archer)",
240237
attribute: "movie_rating",
241238
require: true,
242-
options: [
243-
FormBuilderInputOption(
244-
value: 1,
245-
),
246-
FormBuilderInputOption(
247-
value: 2,
248-
),
249-
FormBuilderInputOption(
250-
value: 3,
251-
),
252-
FormBuilderInputOption(
253-
value: 4,
254-
),
255-
FormBuilderInputOption(
256-
value: 5,
257-
),
258-
FormBuilderInputOption(
259-
value: 6,
260-
),
261-
FormBuilderInputOption(
262-
value: 7,
263-
),
264-
FormBuilderInputOption(
265-
value: 8,
266-
),
267-
FormBuilderInputOption(
268-
value: 9,
269-
),
270-
FormBuilderInputOption(
271-
value: 10,
272-
),
273-
],
274-
),*/
239+
options: List.generate(5, (i) => i + 1)
240+
.map((number) => FormBuilderInputOption(value: number))
241+
.toList(),
242+
),
275243
],
276244
onChanged: () {
277245
print("Form value changed");
@@ -288,3 +256,26 @@ class MyHomePage extends StatelessWidget {
288256
);
289257
}
290258
}
259+
260+
class AppProfile {
261+
final String name;
262+
final String email;
263+
final String imageUrl;
264+
265+
const AppProfile(this.name, this.email, this.imageUrl);
266+
267+
@override
268+
bool operator ==(Object other) =>
269+
identical(this, other) ||
270+
other is AppProfile &&
271+
runtimeType == other.runtimeType &&
272+
name == other.name;
273+
274+
@override
275+
int get hashCode => name.hashCode;
276+
277+
@override
278+
String toString() {
279+
return name;
280+
}
281+
}

0 commit comments

Comments
 (0)