replaceRange method

void replaceRange(
  1. String newValue,
  2. TextRange range
)

Implementation

void replaceRange(String newValue, TextRange range){
  // print("newValue: $newValue, range: $range: ${range.textInside(text)}");
  var newText = text.replaceRange(range.start, range.end, newValue);
  var newRange = TextRange(start: range.start, end: range.start + newValue.length);
  // print("Updated Range Content: [${newRange.textAfter(newText)}], text length: ${newText.length}, ${newRange.end}");
  bool isAtTheEndOfText = (newRange.textAfter(newText) == " " && newRange.end == newText.length - 1);
  if(isAtTheEndOfText){
    newText += " ";
  }
  TextSelection newTextSelection = TextSelection(baseOffset: newRange.end + 1, extentOffset: newRange.end + 1);
  value = value.copyWith(text: newText, selection: newTextSelection);
}