setTextWithCursor method

void setTextWithCursor(
  1. String newText,
  2. int cursorPosition
)

Sets text and cursor position atomically

This method ensures that both text and cursor position are updated together, preventing intermediate states where the cursor might be positioned incorrectly.

newText - The new text content cursorPosition - The desired cursor position (will be clamped to valid range)

Implementation

void setTextWithCursor(String newText, int cursorPosition) {
  final clampedPosition = cursorPosition.clamp(0, newText.length);
  value = TextEditingValue(
    text: newText,
    selection: TextSelection.collapsed(offset: clampedPosition),
  );
}