contraintToNewText function

TextSelection contraintToNewText(
  1. TextEditingValue newValue,
  2. String newText
)

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),
  );
}