Skip to content

Commit 33c9d62

Browse files
authored
fix: Textfield cursor position changes when modifying field content in on_change (#5019)
* initial commit * add comment
1 parent de3b051 commit 33c9d62

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

packages/flet/lib/src/controls/textfield.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ class _TextFieldControlState extends State<TextFieldControl>
8484
setState(() {
8585
_focused = _shiftEnterfocusNode.hasFocus;
8686
});
87-
widget.backend.triggerControlEvent(widget.control.id, _shiftEnterfocusNode.hasFocus ? "focus" : "blur");
87+
widget.backend.triggerControlEvent(
88+
widget.control.id, _shiftEnterfocusNode.hasFocus ? "focus" : "blur");
8889
}
8990

9091
void _onFocusChange() {
@@ -122,7 +123,11 @@ class _TextFieldControlState extends State<TextFieldControl>
122123
String value = widget.control.attrs["value"] ?? "";
123124
if (_value != value) {
124125
_value = value;
125-
_controller.text = value;
126+
_controller.value = TextEditingValue(
127+
text: value,
128+
selection: TextSelection.collapsed(
129+
offset: value.length), // preserve cursor position at the end
130+
);
126131
}
127132

128133
var prefixControls =

0 commit comments

Comments
 (0)