contraintToNewText function
Constrains the text selection to fit within the new text length.
Helper function that ensures selection offsets don't exceed the bounds of the updated text.
Implementation
TextSelection contraintToNewText(TextEditingValue newValue, String newText) {
return TextSelection(
baseOffset: newValue.selection.baseOffset.clamp(0, newText.length),
extentOffset: newValue.selection.extentOffset.clamp(0, newText.length),
);
}