set onChangedField function to only update options with new results (#150776)
Added function and result identifiers into the RawAutocomplete onChangedField method, now the options value will not be updated if the data is from an optionsBuilder call older than the last call which updated the options *List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.* Issue #150774 is fixed by this PR *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
This commit is contained in:
@@ -353,10 +353,28 @@ class _RawAutocompleteState<T extends Object> extends State<RawAutocomplete<T>>
|
||||
}
|
||||
}
|
||||
|
||||
// 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<void> _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<T> 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<T extends Object> extends State<RawAutocomplete<T>>
|
||||
_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.
|
||||
|
||||
@@ -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<String>? lastOptions;
|
||||
Duration? delay;
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: RawAutocomplete<String>(
|
||||
optionsBuilder: (TextEditingValue textEditingValue) async {
|
||||
final Iterable<String> options = kOptions.where((String option) {
|
||||
return option.contains(textEditingValue.text.toLowerCase());
|
||||
});
|
||||
if (delay == null) {
|
||||
return options;
|
||||
}
|
||||
return Future<Iterable<String>>.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<String> onSelected, Iterable<String> 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, <String>['chameleon']);
|
||||
|
||||
// Wait for the last timer to finish.
|
||||
await tester.pump(longRequestDelay - shortRequestDelay);
|
||||
expect(lastOptions, <String>['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<String>? lastOptions;
|
||||
Duration? delay;
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: RawAutocomplete<String>(
|
||||
optionsBuilder: (TextEditingValue textEditingValue) async {
|
||||
final Iterable<String> options = kOptions.where((String option) {
|
||||
return option.contains(textEditingValue.text.toLowerCase());
|
||||
});
|
||||
if (delay == null) {
|
||||
return options;
|
||||
}
|
||||
return Future<Iterable<String>>.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<String> onSelected, Iterable<String> 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, <String>['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<String>? lastOptions;
|
||||
const Duration delay = Duration(milliseconds: 100);
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: RawAutocomplete<String>(
|
||||
optionsBuilder: (TextEditingValue textEditingValue) async {
|
||||
final Iterable<String> options = kOptions.where((String option) {
|
||||
return option.contains(textEditingValue.text.toLowerCase());
|
||||
});
|
||||
return Future<Iterable<String>>.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<String> onSelected, Iterable<String> options) {
|
||||
lastOptions = options;
|
||||
return Container(key: optionsKey);
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
// Setup starting point.
|
||||
await tester.enterText(find.byKey(fieldKey), 'ele');
|
||||
await tester.pump(delay);
|
||||
expect(lastOptions, <String>['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, <String>['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<String>? 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<String> altEleOptions = <String>['something new and crazy for ele!'];
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: RawAutocomplete<String>(
|
||||
optionsBuilder: (TextEditingValue textEditingValue) async {
|
||||
if (textEditingValue.text == 'ele') {
|
||||
timesOptionsBuilderCalledWithEle += 1;
|
||||
if (timesOptionsBuilderCalledWithEle > 1) {
|
||||
return Future<Iterable<String>>.delayed(delay, () => altEleOptions);
|
||||
}
|
||||
}
|
||||
final Iterable<String> options = kOptions.where((String option) {
|
||||
return option.contains(textEditingValue.text.toLowerCase());
|
||||
});
|
||||
return Future<Iterable<String>>.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<String> onSelected, Iterable<String> options) {
|
||||
lastOptions = options;
|
||||
return Container(key: optionsKey);
|
||||
},
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
await tester.enterText(find.byKey(fieldKey), 'ele');
|
||||
await tester.pump(delay);
|
||||
expect(lastOptions, <String>['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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user