Skip to content

Commit 95a6d95

Browse files
RawAutocomplete: Fix focusNode listener mismatch in didUpdateWidget/dispose (flutter#190686)
This is a minor bugfix I'm proposing as prep for my work on flutter#159443. For details on how that work blocks on this, see flutter#183705 (comment). initState registers _onFocusChange on the passed FocusNode, but didUpdateWidget and dispose were removing/adding _updateOptionsViewVisibility instead. This meant the _onFocusChange listener was never properly removed, so it could fire on an already-disposed State. Also, when the focus node changes, refresh _hasFocus from the new node, since its focus state may not match the old node's. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Victor Sanni <[email protected]>
1 parent c3ed85e commit 95a6d95

2 files changed

Lines changed: 294 additions & 3 deletions

File tree

‎packages/flutter/lib/src/widgets/autocomplete.dart‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -696,20 +696,25 @@ class _RawAutocompleteState<T extends Object> extends State<RawAutocomplete<T>>
696696
widget.textEditingController?.addListener(_onChangedField);
697697
}
698698
if (!identical(oldWidget.focusNode, widget.focusNode)) {
699-
oldWidget.focusNode?.removeListener(_updateOptionsViewVisibility);
699+
oldWidget.focusNode?.removeListener(_onFocusChange);
700700
if (oldWidget.focusNode == null) {
701701
_internalFocusNode?.dispose();
702702
_internalFocusNode = null;
703703
}
704-
widget.focusNode?.addListener(_updateOptionsViewVisibility);
704+
widget.focusNode?.addListener(_onFocusChange);
705+
// The new node's focus state may not match the old node's. Update
706+
// _hasFocus, but not the options view visibility, because this method
707+
// runs during the build phase, when OverlayPortalController can't show
708+
// or hide.
709+
_hasFocus = _focusNode.hasFocus;
705710
}
706711
}
707712

708713
@override
709714
void dispose() {
710715
widget.textEditingController?.removeListener(_onChangedField);
711716
_internalTextEditingController?.dispose();
712-
widget.focusNode?.removeListener(_updateOptionsViewVisibility);
717+
widget.focusNode?.removeListener(_onFocusChange);
713718
_internalFocusNode?.dispose();
714719
_highlightedOptionIndex.dispose();
715720
super.dispose();

‎packages/flutter/test/widgets/autocomplete_test.dart‎

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4003,6 +4003,292 @@ void main() {
40034003
expect(targetFocus.hasFocus, isTrue);
40044004
},
40054005
);
4006+
4007+
testWidgets('removes the listener from a caller-provided focusNode on dispose', (
4008+
WidgetTester tester,
4009+
) async {
4010+
final focusNode = _SpyFocusNode();
4011+
addTearDown(focusNode.dispose);
4012+
final textEditingController = TextEditingController();
4013+
addTearDown(textEditingController.dispose);
4014+
4015+
await tester.pumpWidget(
4016+
TestWidgetsApp(
4017+
home: RawAutocomplete<String>(
4018+
focusNode: focusNode,
4019+
textEditingController: textEditingController,
4020+
optionsBuilder: (TextEditingValue textEditingValue) => const <String>[],
4021+
fieldViewBuilder:
4022+
(
4023+
BuildContext context,
4024+
TextEditingController fieldTextEditingController,
4025+
FocusNode fieldFocusNode,
4026+
VoidCallback onFieldSubmitted,
4027+
) {
4028+
return TestTextField(
4029+
focusNode: fieldFocusNode,
4030+
controller: fieldTextEditingController,
4031+
);
4032+
},
4033+
optionsViewBuilder:
4034+
(
4035+
BuildContext context,
4036+
AutocompleteOnSelected<String> onSelected,
4037+
Iterable<String> options,
4038+
) {
4039+
return const SizedBox.shrink();
4040+
},
4041+
),
4042+
),
4043+
);
4044+
expect(focusNode.hasListeners, isTrue);
4045+
4046+
// Unmounting the RawAutocomplete removes everything it added to the
4047+
// caller-owned focusNode, so that it cannot fire on a disposed State.
4048+
await tester.pumpWidget(const TestWidgetsApp(home: SizedBox.shrink()));
4049+
expect(focusNode.hasListeners, isFalse);
4050+
});
4051+
4052+
testWidgets('refreshes the internal focus state when the focusNode changes', (
4053+
WidgetTester tester,
4054+
) async {
4055+
final GlobalKey optionsKey = GlobalKey();
4056+
final focusNode1 = _SpyFocusNode();
4057+
addTearDown(focusNode1.dispose);
4058+
final focusNode2 = _SpyFocusNode();
4059+
addTearDown(focusNode2.dispose);
4060+
final textEditingController = TextEditingController();
4061+
addTearDown(textEditingController.dispose);
4062+
4063+
Future<void> pumpRawAutocomplete(FocusNode focusNode) {
4064+
return tester.pumpWidget(
4065+
TestWidgetsApp(
4066+
home: RawAutocomplete<String>(
4067+
focusNode: focusNode,
4068+
textEditingController: textEditingController,
4069+
optionsBuilder: (TextEditingValue textEditingValue) {
4070+
return kOptions.where((String option) {
4071+
return option.contains(textEditingValue.text.toLowerCase());
4072+
});
4073+
},
4074+
fieldViewBuilder:
4075+
(
4076+
BuildContext context,
4077+
TextEditingController fieldTextEditingController,
4078+
FocusNode fieldFocusNode,
4079+
VoidCallback onFieldSubmitted,
4080+
) {
4081+
return TestTextField(
4082+
focusNode: fieldFocusNode,
4083+
controller: fieldTextEditingController,
4084+
);
4085+
},
4086+
optionsViewBuilder:
4087+
(
4088+
BuildContext context,
4089+
AutocompleteOnSelected<String> onSelected,
4090+
Iterable<String> options,
4091+
) {
4092+
return Container(key: optionsKey);
4093+
},
4094+
),
4095+
),
4096+
);
4097+
}
4098+
4099+
// Focus the field, then enter text that matches no options.
4100+
await pumpRawAutocomplete(focusNode1);
4101+
focusNode1.requestFocus();
4102+
await tester.pump();
4103+
await tester.pump();
4104+
expect(find.byKey(optionsKey), findsOneWidget);
4105+
textEditingController.text = 'xyz';
4106+
await tester.pump();
4107+
expect(find.byKey(optionsKey), findsNothing);
4108+
4109+
// Swap in a focus node that, unlike the old one, is unfocused.
4110+
await pumpRawAutocomplete(focusNode2);
4111+
await tester.pump();
4112+
expect(focusNode2.hasListeners, isTrue);
4113+
expect(focusNode2.hasFocus, isFalse);
4114+
expect(focusNode1.hasListeners, isFalse);
4115+
4116+
// Make options available again, without changing the focus state.
4117+
textEditingController.text = '';
4118+
await tester.pump();
4119+
expect(find.byKey(optionsKey), findsNothing);
4120+
4121+
// Focusing the new node opens the options view: the internal focus state
4122+
// was refreshed at the swap, not left stale from the old node.
4123+
// (The second pump renders the update from the focus-change notification,
4124+
// which can land after the first pump's frame.)
4125+
focusNode2.requestFocus();
4126+
await tester.pump();
4127+
await tester.pump();
4128+
expect(find.byKey(optionsKey), findsOneWidget);
4129+
4130+
// The listener that the swap added to the new node is removed on dispose,
4131+
// like in initState/dispose.
4132+
await tester.pumpWidget(const TestWidgetsApp(home: SizedBox.shrink()));
4133+
expect(focusNode2.hasListeners, isFalse);
4134+
});
4135+
4136+
testWidgets('keeps working when swapping between internal and caller-provided focusNode', (
4137+
WidgetTester tester,
4138+
) async {
4139+
final GlobalKey optionsKey = GlobalKey();
4140+
late FocusNode fieldFocusNode;
4141+
final customFocusNode = FocusNode();
4142+
addTearDown(customFocusNode.dispose);
4143+
final customTextEditingController = TextEditingController();
4144+
addTearDown(customTextEditingController.dispose);
4145+
4146+
// RawAutocomplete requires focusNode and textEditingController to be both
4147+
// null or both non-null (see the dartdoc on those parameters), so they are
4148+
// swapped together.
4149+
Future<void> pumpRawAutocomplete({
4150+
FocusNode? focusNode,
4151+
TextEditingController? textEditingController,
4152+
}) {
4153+
return tester.pumpWidget(
4154+
TestWidgetsApp(
4155+
home: RawAutocomplete<String>(
4156+
focusNode: focusNode,
4157+
textEditingController: textEditingController,
4158+
optionsBuilder: (TextEditingValue textEditingValue) {
4159+
return kOptions.where((String option) {
4160+
return option.contains(textEditingValue.text.toLowerCase());
4161+
});
4162+
},
4163+
fieldViewBuilder:
4164+
(
4165+
BuildContext context,
4166+
TextEditingController fieldTextEditingController,
4167+
FocusNode builderFocusNode,
4168+
VoidCallback onFieldSubmitted,
4169+
) {
4170+
fieldFocusNode = builderFocusNode;
4171+
return TestTextField(
4172+
focusNode: builderFocusNode,
4173+
controller: fieldTextEditingController,
4174+
);
4175+
},
4176+
optionsViewBuilder:
4177+
(
4178+
BuildContext context,
4179+
AutocompleteOnSelected<String> onSelected,
4180+
Iterable<String> options,
4181+
) {
4182+
return Container(key: optionsKey);
4183+
},
4184+
),
4185+
),
4186+
);
4187+
}
4188+
4189+
// The second pump after each focus change renders the update from the
4190+
// focus-change notification, which can land after the first pump's frame.
4191+
Future<void> checkFocusOpensAndClosesOptions(FocusNode focusNode) async {
4192+
focusNode.requestFocus();
4193+
await tester.pump();
4194+
await tester.pump();
4195+
expect(find.byKey(optionsKey), findsOneWidget);
4196+
focusNode.unfocus();
4197+
await tester.pump();
4198+
await tester.pump();
4199+
expect(find.byKey(optionsKey), findsNothing);
4200+
}
4201+
4202+
// Start with the internal focus node (null focusNode parameter).
4203+
await pumpRawAutocomplete();
4204+
await checkFocusOpensAndClosesOptions(fieldFocusNode);
4205+
4206+
// Swap internal → caller-provided.
4207+
await pumpRawAutocomplete(
4208+
focusNode: customFocusNode,
4209+
textEditingController: customTextEditingController,
4210+
);
4211+
await checkFocusOpensAndClosesOptions(customFocusNode);
4212+
4213+
// Swap caller-provided → internal.
4214+
await pumpRawAutocomplete();
4215+
await checkFocusOpensAndClosesOptions(fieldFocusNode);
4216+
});
4217+
4218+
testWidgets('does not update options view visibility when the focusNode changes', (
4219+
WidgetTester tester,
4220+
) async {
4221+
final GlobalKey optionsKey = GlobalKey();
4222+
final focusNode1 = FocusNode();
4223+
addTearDown(focusNode1.dispose);
4224+
final focusNode2 = FocusNode();
4225+
addTearDown(focusNode2.dispose);
4226+
final textEditingController = TextEditingController();
4227+
addTearDown(textEditingController.dispose);
4228+
4229+
Future<void> pumpRawAutocomplete(FocusNode focusNode) {
4230+
return tester.pumpWidget(
4231+
TestWidgetsApp(
4232+
home: RawAutocomplete<String>(
4233+
focusNode: focusNode,
4234+
textEditingController: textEditingController,
4235+
optionsBuilder: (TextEditingValue textEditingValue) {
4236+
return kOptions.where((String option) {
4237+
return option.contains(textEditingValue.text.toLowerCase());
4238+
});
4239+
},
4240+
fieldViewBuilder:
4241+
(
4242+
BuildContext context,
4243+
TextEditingController fieldTextEditingController,
4244+
FocusNode fieldFocusNode,
4245+
VoidCallback onFieldSubmitted,
4246+
) {
4247+
return TestTextField(
4248+
focusNode: fieldFocusNode,
4249+
controller: fieldTextEditingController,
4250+
);
4251+
},
4252+
optionsViewBuilder:
4253+
(
4254+
BuildContext context,
4255+
AutocompleteOnSelected<String> onSelected,
4256+
Iterable<String> options,
4257+
) {
4258+
return Container(key: optionsKey);
4259+
},
4260+
),
4261+
),
4262+
);
4263+
}
4264+
4265+
await pumpRawAutocomplete(focusNode1);
4266+
focusNode1.requestFocus();
4267+
await tester.pump();
4268+
await tester.pump();
4269+
expect(find.byKey(optionsKey), findsOneWidget);
4270+
4271+
// Swap in an unfocused node while the options view is showing. One might
4272+
// expect the view to hide, since the new node is unfocused, but
4273+
// didUpdateWidget runs during the build phase, when
4274+
// OverlayPortalController can't show or hide. So the view stays showing
4275+
// until the next visibility update.
4276+
await pumpRawAutocomplete(focusNode2);
4277+
await tester.pump();
4278+
expect(focusNode2.hasFocus, isFalse);
4279+
expect(find.byKey(optionsKey), findsOneWidget);
4280+
4281+
// The next visibility update, here from a text change, consults the new
4282+
// node's focus state and hides the options view.
4283+
textEditingController.text = 'a';
4284+
await tester.pump();
4285+
expect(find.byKey(optionsKey), findsNothing);
4286+
});
4287+
}
4288+
4289+
class _SpyFocusNode extends FocusNode {
4290+
@override
4291+
bool get hasListeners => super.hasListeners;
40064292
}
40074293

40084294
/// A simple tappable widget used as a replacement for [InkWell] in tests.

0 commit comments

Comments
 (0)