getSentenceFromParagraph method

JidoujishoTextSelection getSentenceFromParagraph(
  1. {required String paragraph,
  2. required int index,
  3. required int startOffset,
  4. required int endOffset}
)

Given paragraph text and an index, yield the part of the text such that the result is a sentence. Different languages may decide to use different delimiters.

Implementation

JidoujishoTextSelection getSentenceFromParagraph({
  required String paragraph,
  required int index,
  required int startOffset,
  required int endOffset,
}) {
  List<String> sentences = getSentences(paragraph);
  int currentIndex = 0;
  String sentenceToReturn = paragraph;

  int sentenceLength = 0;

  for (String sentence in sentences) {
    sentenceToReturn = sentence;
    sentenceLength = sentence.length;

    currentIndex += sentenceLength;
    if (currentIndex > index) {
      break;
    }
  }

  TextRange range = TextRange(
    start: sentenceLength - currentIndex + startOffset,
    end: sentenceLength - currentIndex + endOffset,
  );
  return JidoujishoTextSelection(
    text: sentenceToReturn,
    range: range,
  );
}