diff --git a/packages/flutter/lib/src/widgets/autocomplete.dart b/packages/flutter/lib/src/widgets/autocomplete.dart index db01482c02..2319ecbfc0 100644 --- a/packages/flutter/lib/src/widgets/autocomplete.dart +++ b/packages/flutter/lib/src/widgets/autocomplete.dart @@ -316,6 +316,7 @@ class _RawAutocompleteState extends State> _userHidOptions = false; _lastFieldText = value.text; } + _updateActions(); _updateOverlay(); } @@ -323,6 +324,7 @@ class _RawAutocompleteState extends State> void _onChangedFocus() { // Options should no longer be hidden when the field is re-focused. _userHidOptions = !_focusNode.hasFocus; + _updateActions(); _updateOverlay(); } @@ -345,6 +347,8 @@ class _RawAutocompleteState extends State> selection: TextSelection.collapsed(offset: selectionString.length), text: selectionString, ); + _updateActions(); + _updateOverlay(); widget.onSelected?.call(_selection!); } @@ -355,6 +359,7 @@ class _RawAutocompleteState extends State> void _highlightPreviousOption(AutocompletePreviousOptionIntent intent) { if (_userHidOptions) { _userHidOptions = false; + _updateActions(); _updateOverlay(); return; } @@ -364,6 +369,7 @@ class _RawAutocompleteState extends State> void _highlightNextOption(AutocompleteNextOptionIntent intent) { if (_userHidOptions) { _userHidOptions = false; + _updateActions(); _updateOverlay(); return; } @@ -373,6 +379,7 @@ class _RawAutocompleteState extends State> Object? _hideOptions(DismissIntent intent) { if (!_userHidOptions) { _userHidOptions = true; + _updateActions(); _updateOverlay(); return null; } @@ -389,12 +396,27 @@ class _RawAutocompleteState extends State> _hideOptionsAction.enabled = enabled; } + void _updateActions() { + _setActionsEnabled(_focusNode.hasFocus && _selection == null && _options.isNotEmpty); + } + + bool _floatingOptionsUpdateScheduled = false; // Hide or show the options overlay, if needed. void _updateOverlay() { - _setActionsEnabled(_focusNode.hasFocus && _selection == null && _options.isNotEmpty); + if (SchedulerBinding.instance.schedulerPhase == SchedulerPhase.persistentCallbacks) { + if (!_floatingOptionsUpdateScheduled) { + _floatingOptionsUpdateScheduled = true; + SchedulerBinding.instance.addPostFrameCallback((Duration timeStamp) { + _floatingOptionsUpdateScheduled = false; + _updateOverlay(); + }); + } + return; + } + + _floatingOptions?.remove(); if (_shouldShowOptions) { - _floatingOptions?.remove(); - _floatingOptions = OverlayEntry( + final OverlayEntry newFloatingOptions = OverlayEntry( builder: (BuildContext context) { return CompositedTransformFollower( link: _optionsLayerLink, @@ -411,9 +433,9 @@ class _RawAutocompleteState extends State> ); }, ); - Overlay.of(context, rootOverlay: true)!.insert(_floatingOptions!); - } else if (_floatingOptions != null) { - _floatingOptions!.remove(); + Overlay.of(context, rootOverlay: true)!.insert(newFloatingOptions); + _floatingOptions = newFloatingOptions; + } else { _floatingOptions = null; } } @@ -473,9 +495,8 @@ class _RawAutocompleteState extends State> AutocompleteNextOptionIntent: _nextOptionAction, DismissIntent: _hideOptionsAction, }; - SchedulerBinding.instance.addPostFrameCallback((Duration _) { - _updateOverlay(); - }); + _updateActions(); + _updateOverlay(); } @override @@ -486,9 +507,8 @@ class _RawAutocompleteState extends State> widget.textEditingController, ); _updateFocusNode(oldWidget.focusNode, widget.focusNode); - SchedulerBinding.instance.addPostFrameCallback((Duration _) { - _updateOverlay(); - }); + _updateActions(); + _updateOverlay(); } @override diff --git a/packages/flutter/test/widgets/autocomplete_test.dart b/packages/flutter/test/widgets/autocomplete_test.dart index 00368b099c..66c5e1188f 100644 --- a/packages/flutter/test/widgets/autocomplete_test.dart +++ b/packages/flutter/test/widgets/autocomplete_test.dart @@ -1023,4 +1023,53 @@ void main() { expect(lastHighlighted, 5); }); + testWidgets('floating menu goes away on select', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/99749. + final GlobalKey fieldKey = GlobalKey(); + final GlobalKey optionsKey = GlobalKey(); + late AutocompleteOnSelected lastOnSelected; + late FocusNode focusNode; + late TextEditingController textEditingController; + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: RawAutocomplete( + optionsBuilder: (TextEditingValue textEditingValue) { + return kOptions.where((String option) { + return option.contains(textEditingValue.text.toLowerCase()); + }); + }, + 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) { + lastOnSelected = onSelected; + return Container(key: optionsKey); + }, + ), + ), + ), + ); + + // The field is always rendered, but the options are not unless needed. + expect(find.byKey(fieldKey), findsOneWidget); + expect(find.byKey(optionsKey), findsNothing); + + await tester.enterText(find.byKey(fieldKey), kOptions[0]); + await tester.pumpAndSettle(); + expect(find.byKey(optionsKey), findsOneWidget); + + // Pretend that the only option is selected. This does not change the + // text in the text field. + lastOnSelected(kOptions[0]); + await tester.pump(); + expect(find.byKey(optionsKey), findsNothing); + }); }