buildTextSpanWithSpellCheckSuggestions function

TextSpan buildTextSpanWithSpellCheckSuggestions(
  1. TextEditingValue value,
  2. bool composingWithinCurrentTextRange,
  3. TextStyle? style,
  4. TextStyle misspelledTextStyle,
  5. SpellCheckResults spellCheckResults,
)

Builds the TextSpan tree given the current state of the text input and spell check results.

The value is the current TextEditingValue requested to be rendered by a text input widget. The composingWithinCurrentTextRange value represents whether or not there is a valid composing region in the value. The style is the TextStyle to render the value's text with, and the misspelledTextStyle is the TextStyle to render misspelled words within the value's text with. The spellCheckResults are the results of spell checking the value's text.

Implementation

TextSpan buildTextSpanWithSpellCheckSuggestions(
    TextEditingValue value,
    bool composingWithinCurrentTextRange,
    TextStyle? style,
    TextStyle misspelledTextStyle,
    SpellCheckResults spellCheckResults) {
  List<SuggestionSpan> spellCheckResultsSpans =
      spellCheckResults.suggestionSpans;
  final String spellCheckResultsText = spellCheckResults.spellCheckedText;

  if (spellCheckResultsText != value.text) {
    spellCheckResultsSpans = _correctSpellCheckResults(
        value.text, spellCheckResultsText, spellCheckResultsSpans);
  }

  // We will draw the TextSpan tree based on the composing region, if it is
  // available.
  // TODO(camsim99): The two separate strategies for building TextSpan trees
  // based on the availability of a composing region should be merged:
  // https://github.com/flutter/flutter/issues/124142.
  final bool shouldConsiderComposingRegion = defaultTargetPlatform == TargetPlatform.android;
  if (shouldConsiderComposingRegion) {
    return TextSpan(
      style: style,
      children: _buildSubtreesWithComposingRegion(
          spellCheckResultsSpans,
          value,
          style,
          misspelledTextStyle,
          composingWithinCurrentTextRange,
      ),
    );
  }

  return TextSpan(
    style: style,
    children: _buildSubtreesWithoutComposingRegion(
      spellCheckResultsSpans,
      value,
      style,
      misspelledTextStyle,
      value.selection.baseOffset,
    ),
  );
}