You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
A way to distinguish TextEditingController event types. #79982
When using the TextEditingController.addListener(myListener) I get events from text changes but also from focus changes.
If I want to change my state, let's say showing an alert at some point when the user is typing, myListener is called 2 more times.
Meaning that I need to keep track of the previous text and check every time if the text did actually change or if it was just a focus event.
Same happens when the TextField get focused or unfocused. I get calls to myListener.
Proposal
Having two listener types for TextEditingController. One for focus events and another one just for text changes.
@justinmc I'm wondering whether this this is should be considered as an issue rather than a proposal to distinguish event types. From my understanding controller is expected to be notified when the text changes only and not on focus change (in the user perspective).
I'm considering this a bug for now, feel free to update the labels as required.
I believe that having two events is better than just removing the focus event because it would give us more control over the textfield. But I'm used to event driven platforms, and flutter isn't that event driven, so the dev team will know what's best.
I think one of the event is triggered by selection changes? The text editing value includes both selection and text (as well as composing). When a textfield gets focused, the selection changes from TextSelection.collapsed(offset: -1) to TextSelection.collapsed(offset: text.length)
Yeah I think the behavior of calling the listener for both selection and text changes is working as intended, so this is a proposal and not a bug. TextEditingValue includes both selection and text as @chunhtai said, and since TextEditingController is just a ValueNotifier, it will call the listener whenever the TextEditingValue changes at all.
Activity
pedromassangocode commentedon Apr 8, 2021
Related to #64143 and #79982.
@justinmc I'm wondering whether this this is should be considered as an issue rather than a proposal to distinguish event types. From my understanding controller is expected to be notified when the text changes only and not on focus change (in the user perspective).
I'm considering this a bug for now, feel free to update the labels as required.
Cc @LongCatIsLooong @chunhtai
The code bellow prints every time the text field gains/loses focus.
reproducible code sample
dpedrinha commentedon Apr 8, 2021
I believe that having two events is better than just removing the focus event because it would give us more control over the textfield. But I'm used to event driven platforms, and flutter isn't that event driven, so the dev team will know what's best.
chunhtai commentedon Apr 8, 2021
I think one of the event is triggered by selection changes? The text editing value includes both selection and text (as well as composing). When a textfield gets focused, the selection changes from TextSelection.collapsed(offset: -1) to TextSelection.collapsed(offset: text.length)
justinmc commentedon Apr 8, 2021
Yeah I think the behavior of calling the listener for both selection and text changes is working as intended, so this is a proposal and not a bug. TextEditingValue includes both selection and text as @chunhtai said, and since TextEditingController is just a ValueNotifier, it will call the listener whenever the TextEditingValue changes at all.
The workaround mentioned in the description above: https://dartpad.dartlang.org/eec5ab37140ce52c8eb530b861960907
I wonder if it would be possible to write a subclass of TextEditingController that has
addTextListenerandaddSelectionListener?chunhtai commentedon Apr 8, 2021
Just a FYI, there is a onChange parameter for textfield that is specifically for text changes.
LongCatIsLooong commentedon Apr 9, 2021
IIRC when
EditableTextgets unfocused it sets the selection to(-1, -1). It's probably not needed anymore. Let me see if I can remove it.17 remaining items
flutter-triage-bot commentedon Sep 17, 2023
This issue is missing a priority label. Please set a priority label when adding the
triaged-frameworklabel.