insertText method
Inserts a string at the current cursor position of the active text field. If text selection is active, replaces the selected range.
Implementation
void insertText(String value) {
final field = _focusedField;
if (field == null) return;
final controller = controllers[field];
if (controller == null) return;
final currentText = controller.text;
final selection = controller.selection;
int start = selection.start;
int end = selection.end;
// Fallback if cursor position is not set/invalid
if (start < 0 || end < 0) {
start = currentText.length;
end = currentText.length;
}
final newText = currentText.replaceRange(start, end, value);
final newCursorPosition = start + value.length;
controller.value = TextEditingValue(
text: newText,
selection: TextSelection.collapsed(offset: newCursorPosition),
);
notifyListeners();
}