Cause:
This usually happens because the KeyboardListener or internal focus handling conflicts with the default behavior of the text field, especially when managing tags dynamically. Sometimes the event is intercepted and the text field doesn’t get the backspace event properly.
Fix Suggestion:
Ensure the backspace event only removes the last tag when the input is empty, otherwise let the text field handle character deletion normally.
Use RawKeyboardListener or KeyboardListener carefully — only intercept the event when the text is empty and tags are present.
Explicitly call setState or update the controller after tag removal to reflect changes immediately.
Double-check the focus node and controller are properly assigned and synchronized.
KeyboardListener(
focusNode: inputFieldValues.focusNode,
onKeyEvent: (event) {
if (event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.backspace) {
final text = inputFieldValues.textEditingController.text;
if (text.isEmpty && inputFieldValues.tags.isNotEmpty) {
// Remove last tag only when text field is empty
inputFieldValues.onTagRemoved(inputFieldValues.tags.last);
} else {
// Let TextField handle the backspace normally
}
}
},
child: /* your Wrap or widget here */,
);