From 40b33e238bb528434163cd37514053910323e77b Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 9 Sep 2021 00:17:04 +0200 Subject: [PATCH] Autocomplete: support asynchronous options (#88193) --- .../flutter/lib/src/widgets/autocomplete.dart | 8 ++- .../test/widgets/autocomplete_test.dart | 61 +++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/packages/flutter/lib/src/widgets/autocomplete.dart b/packages/flutter/lib/src/widgets/autocomplete.dart index 3e95138201..863aee3594 100644 --- a/packages/flutter/lib/src/widgets/autocomplete.dart +++ b/packages/flutter/lib/src/widgets/autocomplete.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:async'; + import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; @@ -22,7 +24,7 @@ import 'shortcuts.dart'; /// See also: /// /// * [RawAutocomplete.optionsBuilder], which is of this type. -typedef AutocompleteOptionsBuilder = Iterable Function(TextEditingValue textEditingValue); +typedef AutocompleteOptionsBuilder = FutureOr> Function(TextEditingValue textEditingValue); /// The type of the callback used by the [RawAutocomplete] widget to indicate /// that the user has selected an option. @@ -293,8 +295,8 @@ class _RawAutocompleteState extends State> } // Called when _textEditingController changes. - void _onChangedField() { - final Iterable options = widget.optionsBuilder( + Future _onChangedField() async { + final Iterable options = await widget.optionsBuilder( _textEditingController.value, ); _options = options; diff --git a/packages/flutter/test/widgets/autocomplete_test.dart b/packages/flutter/test/widgets/autocomplete_test.dart index 9ab8bd7d06..8c26983428 100644 --- a/packages/flutter/test/widgets/autocomplete_test.dart +++ b/packages/flutter/test/widgets/autocomplete_test.dart @@ -647,6 +647,67 @@ void main() { ); }); + testWidgets('support asynchronous options builder', (WidgetTester tester) async { + final GlobalKey fieldKey = GlobalKey(); + final GlobalKey optionsKey = GlobalKey(); + late FocusNode focusNode; + late TextEditingController textEditingController; + Iterable? lastOptions; + Duration? delay; + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: RawAutocomplete( + optionsBuilder: (TextEditingValue textEditingValue) async { + final Iterable options = kOptions.where((String option) { + return option.contains(textEditingValue.text.toLowerCase()); + }); + if (delay == null) { + return options; + } + return Future>.delayed(delay, () => options); + }, + fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) { + focusNode = fieldFocusNode; + textEditingController = fieldTextEditingController; + return TextField( + key: fieldKey, + focusNode: focusNode, + controller: textEditingController, + ); + }, + optionsViewBuilder: (BuildContext context, AutocompleteOnSelected onSelected, Iterable options) { + lastOptions = options; + return Container(key: optionsKey); + }, + ), + ), + ) + ); + + // Enter text to build the options with delay. + focusNode.requestFocus(); + delay = const Duration(milliseconds: 500); + await tester.enterText(find.byKey(fieldKey), 'go'); + await tester.pumpAndSettle(); + + // The options have not yet been built. + expect(find.byKey(optionsKey), findsNothing); + expect(lastOptions, isNull); + + // Await asynchronous options builder. + await tester.pumpAndSettle(delay); + expect(find.byKey(optionsKey), findsOneWidget); + expect(lastOptions, ['dingo', 'flamingo', 'goose']); + + // Enter text to rebuild the options without delay. + delay = null; + await tester.enterText(find.byKey(fieldKey), 'ngo'); + await tester.pump(); + expect(lastOptions, ['dingo', 'flamingo']); + }); + testWidgets('can navigate options with the keyboard', (WidgetTester tester) async { final GlobalKey fieldKey = GlobalKey(); final GlobalKey optionsKey = GlobalKey();