replaceText method
Replaces the text while preserving selection within bounds.
Adjusts the selection to stay within the new text length.
Parameters:
newText(String, required): Replacement text.
Returns: TextEditingValue — value with new text and adjusted selection.
Implementation
TextEditingValue replaceText(String newText) {
var selection = this.selection;
selection = selection.copyWith(
baseOffset: selection.baseOffset.clamp(0, newText.length),
extentOffset: selection.extentOffset.clamp(0, newText.length),
);
return TextEditingValue(
text: newText,
selection: selection,
);
}