@@ -129,6 +129,76 @@ void main() {
129129 expect (lastOptions.elementAt (5 ), 'northern white rhinoceros' );
130130 });
131131
132+ testWidgets ('tapping on an option selects it' , (WidgetTester tester) async {
133+ final GlobalKey fieldKey = GlobalKey ();
134+ final GlobalKey optionsKey = GlobalKey ();
135+ late Iterable <String > lastOptions;
136+ late FocusNode focusNode;
137+ late TextEditingController textEditingController;
138+
139+ await tester.pumpWidget (
140+ MaterialApp (
141+ home: Scaffold (
142+ body: RawAutocomplete <String >(
143+ optionsBuilder: (TextEditingValue textEditingValue) {
144+ return kOptions.where ((String option) {
145+ return option.contains (textEditingValue.text.toLowerCase ());
146+ });
147+ },
148+ fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
149+ focusNode = fieldFocusNode;
150+ textEditingController = fieldTextEditingController;
151+ return TextField (
152+ key: fieldKey,
153+ focusNode: focusNode,
154+ controller: textEditingController,
155+ );
156+ },
157+ optionsViewBuilder: (BuildContext context, AutocompleteOnSelected <String > onSelected, Iterable <String > options) {
158+ lastOptions = options;
159+ return Material (
160+ elevation: 4.0 ,
161+ child: ListView .builder (
162+ key: optionsKey,
163+ padding: const EdgeInsets .all (8.0 ),
164+ itemCount: options.length,
165+ itemBuilder: (BuildContext context, int index) {
166+ final String option = options.elementAt (index);
167+ return GestureDetector (
168+ onTap: () {
169+ onSelected (option);
170+ },
171+ child: ListTile (
172+ title: Text (option),
173+ ),
174+ );
175+ },
176+ ),
177+ );
178+ },
179+ ),
180+ ),
181+ ),
182+ );
183+
184+ // The field is always rendered, but the options are not unless needed.
185+ expect (find.byKey (fieldKey), findsOneWidget);
186+ expect (find.byKey (optionsKey), findsNothing);
187+
188+ // Tap on the text field to open the options.
189+ await tester.tap (find.byKey (fieldKey));
190+ await tester.pump ();
191+ expect (find.byKey (optionsKey), findsOneWidget);
192+ expect (lastOptions.length, kOptions.length);
193+
194+ await tester.tap (find.text (kOptions[2 ]));
195+ await tester.pump ();
196+
197+ expect (find.byKey (optionsKey), findsNothing);
198+
199+ expect (textEditingController.text, equals (kOptions[2 ]));
200+ });
201+
132202 testWidgets ('can filter and select a list of custom User options' , (WidgetTester tester) async {
133203 final GlobalKey fieldKey = GlobalKey ();
134204 final GlobalKey optionsKey = GlobalKey ();
0 commit comments