Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 1 | // 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 Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 5 | import 'package:flutter/material.dart'; |
| 6 | |
Greg Spencer | e3bc8ef | 2023-04-04 13:34:29 -0700 | [diff] [blame] | 7 | /// Flutter code sample for [Autocomplete]. |
| 8 | |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 9 | void main() => runApp(const AutocompleteExampleApp()); |
| 10 | |
| 11 | class AutocompleteExampleApp extends StatelessWidget { |
Michael Goderbauer | 2cbad4b | 2022-05-17 15:35:07 -0700 | [diff] [blame] | 12 | const AutocompleteExampleApp({super.key}); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 13 | |
| 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 McCandless | f6f5bb9 | 2023-05-22 09:55:21 -0700 | [diff] [blame] | 21 | 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 Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 29 | ), |
| 30 | ), |
| 31 | ); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | @immutable |
| 36 | class 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 Khatri | 671aa9e | 2022-03-05 02:26:21 +0530 | [diff] [blame] | 59 | int get hashCode => Object.hash(email, name); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | class AutocompleteBasicUserExample extends StatelessWidget { |
Michael Goderbauer | 2cbad4b | 2022-05-17 15:35:07 -0700 | [diff] [blame] | 63 | const AutocompleteBasicUserExample({super.key}); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 64 | |
| 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 Spencer | e3bc8ef | 2023-04-04 13:34:29 -0700 | [diff] [blame] | 82 | return option.toString().contains(textEditingValue.text.toLowerCase()); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 83 | }); |
| 84 | }, |
| 85 | onSelected: (User selection) { |
Ian Hickson | f25b833 | 2021-10-07 16:48:04 -0700 | [diff] [blame] | 86 | debugPrint('You just selected ${_displayStringForOption(selection)}'); |
Greg Spencer | 33403bd | 2021-08-25 09:45:12 -0700 | [diff] [blame] | 87 | }, |
| 88 | ); |
| 89 | } |
| 90 | } |