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 | |
5 | import 'package:flutter/material.dart'; |
6 | |
7 | /// Flutter code sample for [RawAutocomplete]. |
8 | |
9 | void main() => runApp(const AutocompleteExampleApp()); |
10 | |
11 | class AutocompleteExampleApp extends StatelessWidget { |
12 | const AutocompleteExampleApp({super.key}); |
13 | |
14 | @override |
15 | Widget build(BuildContext context) { |
16 | return MaterialApp( |
17 | home: Scaffold( |
18 | appBar: AppBar( |
19 | title: const Text('RawAutocomplete Basic'), |
20 | ), |
21 | body: const Center( |
22 | child: AutocompleteBasicExample(), |
23 | ), |
24 | ), |
25 | ); |
26 | } |
27 | } |
28 | |
29 | class AutocompleteBasicExample extends StatelessWidget { |
30 | const AutocompleteBasicExample({super.key}); |
31 | |
32 | static const List<String> _options = <String>[ |
33 | 'aardvark', |
34 | 'bobcat', |
35 | 'chameleon', |
36 | ]; |
37 | |
38 | @override |
39 | Widget build(BuildContext context) { |
40 | return RawAutocomplete<String>( |
41 | optionsBuilder: (TextEditingValue textEditingValue) { |
42 | return _options.where((String option) { |
43 | return option.contains(textEditingValue.text.toLowerCase()); |
44 | }); |
45 | }, |
46 | fieldViewBuilder: ( |
47 | BuildContext context, |
48 | TextEditingController textEditingController, |
49 | FocusNode focusNode, |
50 | VoidCallback onFieldSubmitted, |
51 | ) { |
52 | return TextFormField( |
53 | controller: textEditingController, |
54 | focusNode: focusNode, |
55 | onFieldSubmitted: (String value) { |
56 | onFieldSubmitted(); |
57 | }, |
58 | ); |
59 | }, |
60 | optionsViewBuilder: ( |
61 | BuildContext context, |
62 | AutocompleteOnSelected<String> onSelected, |
63 | Iterable<String> options, |
64 | ) { |
65 | return Align( |
66 | alignment: Alignment.topLeft, |
67 | child: Material( |
68 | elevation: 4.0, |
69 | child: SizedBox( |
70 | height: 200.0, |
71 | child: ListView.builder( |
72 | padding: const EdgeInsets.all(8.0), |
73 | itemCount: options.length, |
74 | itemBuilder: (BuildContext context, int index) { |
75 | final String option = options.elementAt(index); |
76 | return GestureDetector( |
77 | onTap: () { |
78 | onSelected(option); |
79 | }, |
80 | child: ListTile( |
81 | title: Text(option), |
82 | ), |
83 | ); |
84 | }, |
85 | ), |
86 | ), |
87 | ), |
88 | ); |
89 | }, |
90 | ); |
91 | } |
92 | } |
93 |