blob: abee231da462f4eb1f051936dea8db9a35d9a98d [file] [log] [blame]
Greg Spencer33403bd2021-08-25 09:45:12 -07001// Copyright 2014 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Greg Spencer33403bd2021-08-25 09:45:12 -07005import 'package:flutter/material.dart';
6
Greg Spencere3bc8ef2023-04-04 13:34:29 -07007/// Flutter code sample for [Autocomplete].
8
Greg Spencer33403bd2021-08-25 09:45:12 -07009void main() => runApp(const AutocompleteExampleApp());
10
11class AutocompleteExampleApp extends StatelessWidget {
Michael Goderbauer2cbad4b2022-05-17 15:35:07 -070012 const AutocompleteExampleApp({super.key});
Greg Spencer33403bd2021-08-25 09:45:12 -070013
14 @override
15 Widget build(BuildContext context) {
16 return MaterialApp(
17 home: Scaffold(
18 appBar: AppBar(
19 title: const Text('Autocomplete Basic User'),
20 ),
Justin McCandlessf6f5bb92023-05-22 09:55:21 -070021 body: Center(
22 child: Column(
23 mainAxisAlignment: MainAxisAlignment.center,
24 children: <Widget>[
25 Text('Type below to autocomplete the following possible results: ${AutocompleteBasicUserExample._userOptions}.'),
26 const AutocompleteBasicUserExample(),
27 ],
28 ),
Greg Spencer33403bd2021-08-25 09:45:12 -070029 ),
30 ),
31 );
32 }
33}
34
35@immutable
36class User {
37 const User({
38 required this.email,
39 required this.name,
40 });
41
42 final String email;
43 final String name;
44
45 @override
46 String toString() {
47 return '$name, $email';
48 }
49
50 @override
51 bool operator ==(Object other) {
52 if (other.runtimeType != runtimeType) {
53 return false;
54 }
55 return other is User && other.name == name && other.email == email;
56 }
57
58 @override
Viren Khatri671aa9e2022-03-05 02:26:21 +053059 int get hashCode => Object.hash(email, name);
Greg Spencer33403bd2021-08-25 09:45:12 -070060}
61
62class AutocompleteBasicUserExample extends StatelessWidget {
Michael Goderbauer2cbad4b2022-05-17 15:35:07 -070063 const AutocompleteBasicUserExample({super.key});
Greg Spencer33403bd2021-08-25 09:45:12 -070064
65 static const List<User> _userOptions = <User>[
66 User(name: 'Alice', email: 'alice@example.com'),
67 User(name: 'Bob', email: 'bob@example.com'),
68 User(name: 'Charlie', email: 'charlie123@gmail.com'),
69 ];
70
71 static String _displayStringForOption(User option) => option.name;
72
73 @override
74 Widget build(BuildContext context) {
75 return Autocomplete<User>(
76 displayStringForOption: _displayStringForOption,
77 optionsBuilder: (TextEditingValue textEditingValue) {
78 if (textEditingValue.text == '') {
79 return const Iterable<User>.empty();
80 }
81 return _userOptions.where((User option) {
Greg Spencere3bc8ef2023-04-04 13:34:29 -070082 return option.toString().contains(textEditingValue.text.toLowerCase());
Greg Spencer33403bd2021-08-25 09:45:12 -070083 });
84 },
85 onSelected: (User selection) {
Ian Hicksonf25b8332021-10-07 16:48:04 -070086 debugPrint('You just selected ${_displayStringForOption(selection)}');
Greg Spencer33403bd2021-08-25 09:45:12 -070087 },
88 );
89 }
90}