diff --git a/packages/flutter/lib/src/widgets/autocomplete.dart b/packages/flutter/lib/src/widgets/autocomplete.dart index 5496c46866..bda02c2a7f 100644 --- a/packages/flutter/lib/src/widgets/autocomplete.dart +++ b/packages/flutter/lib/src/widgets/autocomplete.dart @@ -353,10 +353,28 @@ class _RawAutocompleteState extends State> } } + // Assigning an ID to every call of _onChangedField is necessary to avoid a + // situation where _options is updated by an older call when multiple + // _onChangedField calls are running simultaneously. + int _onChangedCallId = 0; // Called when _textEditingController changes. Future _onChangedField() async { final TextEditingValue value = _textEditingController.value; + + // Makes sure that options change only when content of the field changes. + bool shouldUpdateOptions = false; + if (value.text != _lastFieldText) { + shouldUpdateOptions = true; + _onChangedCallId += 1; + } + _lastFieldText = value.text; + final int callId = _onChangedCallId; final Iterable options = await widget.optionsBuilder(value); + + // Makes sure that previous call results do not replace new ones. + if (callId != _onChangedCallId || !shouldUpdateOptions) { + return; + } _options = options; _updateHighlight(_highlightedOptionIndex.value); final T? selection = _selection; @@ -364,12 +382,7 @@ class _RawAutocompleteState extends State> _selection = null; } - // Make sure the options are no longer hidden if the content of the field - // changes (ignore selection changes). - if (value.text != _lastFieldText) { - _lastFieldText = value.text; - _updateOptionsViewVisibility(); - } + _updateOptionsViewVisibility(); } // Called from fieldViewBuilder when the user submits the field. diff --git a/packages/flutter/test/widgets/autocomplete_test.dart b/packages/flutter/test/widgets/autocomplete_test.dart index cf83d5abc4..092780779a 100644 --- a/packages/flutter/test/widgets/autocomplete_test.dart +++ b/packages/flutter/test/widgets/autocomplete_test.dart @@ -1288,4 +1288,274 @@ void main() { await tester.pump(); expect(find.byKey(optionsKey), findsNothing); }); + + testWidgets('can prevent older optionsBuilder results from replacing the new ones', (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); + }, + ), + ), + ) + ); + + const Duration longRequestDelay = Duration(milliseconds: 5000); + const Duration shortRequestDelay = Duration(milliseconds: 1000); + focusNode.requestFocus(); + + // Enter the first letter. + delay = longRequestDelay; + await tester.enterText(find.byKey(fieldKey), 'c'); + await tester.pump(); + expect(lastOptions, null); + + // Enter the second letter which resolves faster. + delay = shortRequestDelay; + await tester.enterText(find.byKey(fieldKey), 'ch'); + await tester.pump(); + expect(lastOptions, null); + + // Wait for the short request to resolve. + await tester.pump(shortRequestDelay); + + // lastOptions must contain results from the last request. + expect(find.byKey(optionsKey), findsOneWidget); + expect(lastOptions, ['chameleon']); + + // Wait for the last timer to finish. + await tester.pump(longRequestDelay - shortRequestDelay); + expect(lastOptions, ['chameleon']); + }); + + testWidgets('updates result only from the last call made to optionsBuilder', (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); + }, + ), + ), + ) + ); + + focusNode.requestFocus(); + const Duration firstRequestDelay = Duration(milliseconds: 1000); + const Duration secondRequestDelay = Duration(milliseconds: 2000); + const Duration thirdRequestDelay = Duration(milliseconds: 3000); + + // Enter the first letter. + delay = firstRequestDelay; + await tester.enterText(find.byKey(fieldKey), 'l'); + await tester.pump(); + expect(lastOptions, null); + + // Enter the second letter which resolves slower. + delay = secondRequestDelay; + await tester.enterText(find.byKey(fieldKey), 'le'); + await tester.pump(); + expect(lastOptions, null); + + // Enter the third letter which resolves the slowest. + delay = thirdRequestDelay; + await tester.enterText(find.byKey(fieldKey), 'lem'); + await tester.pump(); + expect(lastOptions, null); + + // lastOptions should get updated only from the last request. + await tester.pump(firstRequestDelay); + expect(find.byKey(optionsKey), findsNothing); + expect(lastOptions, null); + + await tester.pump(secondRequestDelay - firstRequestDelay); + expect(find.byKey(optionsKey), findsNothing); + expect(lastOptions, null); + + await tester.pump(thirdRequestDelay - secondRequestDelay); + expect(find.byKey(optionsKey), findsOneWidget); + expect(lastOptions, ['lemur']); + }); + + testWidgets('update options view when field input changes return to the starting keyword', (WidgetTester tester) async { + final GlobalKey fieldKey = GlobalKey(); + final GlobalKey optionsKey = GlobalKey(); + late FocusNode focusNode; + late TextEditingController textEditingController; + Iterable? lastOptions; + const Duration delay = Duration(milliseconds: 100); + + 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()); + }); + 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); + }, + ), + ), + ) + ); + + // Setup starting point. + await tester.enterText(find.byKey(fieldKey), 'ele'); + await tester.pump(delay); + expect(lastOptions, ['chameleon', 'elephant']); + expect(find.byKey(optionsKey), findsOneWidget); + + // Hide options. + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pump(); + expect(find.byKey(optionsKey), findsNothing); + + // Enter another letter and then immediately erase it. + await tester.enterText(find.byKey(fieldKey), 'eleo'); + await tester.pump(); + expect(find.byKey(optionsKey), findsNothing); + + await tester.enterText(find.byKey(fieldKey), 'ele'); + await tester.pump(delay); + expect(lastOptions, ['chameleon', 'elephant']); + + // Options dropdown should be visible after the last optionsBuilder + // call is resolved. + expect(find.byKey(optionsKey), findsOneWidget); + }); + + testWidgets('optionsBuilder does not have to be a pure function', (WidgetTester tester) async { + final GlobalKey fieldKey = GlobalKey(); + final GlobalKey optionsKey = GlobalKey(); + late FocusNode focusNode; + late TextEditingController textEditingController; + Iterable? lastOptions; + const Duration delay = Duration(milliseconds: 100); + + // This is used to tell optionsBuilder to return something different after + // being called with "ele" the second time. I.e. it is not a pure function. + int timesOptionsBuilderCalledWithEle = 0; + final Iterable altEleOptions = ['something new and crazy for ele!']; + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: RawAutocomplete( + optionsBuilder: (TextEditingValue textEditingValue) async { + if (textEditingValue.text == 'ele') { + timesOptionsBuilderCalledWithEle += 1; + if (timesOptionsBuilderCalledWithEle > 1) { + return Future>.delayed(delay, () => altEleOptions); + } + } + final Iterable options = kOptions.where((String option) { + return option.contains(textEditingValue.text.toLowerCase()); + }); + 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); + }, + ), + ), + ) + ); + + await tester.enterText(find.byKey(fieldKey), 'ele'); + await tester.pump(delay); + expect(lastOptions, ['chameleon', 'elephant']); + expect(find.byKey(optionsKey), findsOneWidget); + + await tester.sendKeyEvent(LogicalKeyboardKey.escape); + await tester.pump(); + expect(find.byKey(optionsKey), findsNothing); + + await tester.enterText(find.byKey(fieldKey), 'eleo'); + await tester.pump(); + expect(find.byKey(optionsKey), findsNothing); + + await tester.enterText(find.byKey(fieldKey), 'ele'); + await tester.pump(delay); + expect(lastOptions, altEleOptions); + expect(find.byKey(optionsKey), findsOneWidget); + }); }