buildTextSpan method

  1. @override
TextSpan buildTextSpan({
  1. required BuildContext context,
  2. TextStyle? style,
  3. required bool withComposing,
})
override

Builds TextSpan from current editing value.

By default makes text in composing range appear as underlined. Descendants can override this method to customize appearance of text.

Implementation

@override
TextSpan buildTextSpan({
  required BuildContext context,
  TextStyle? style,
  required bool withComposing,
}) {
  assert(!value.composing.isValid ||
      !withComposing ||
      value.isComposingRangeValid);
  // If the composing range is out of range for the current text, ignore it to
  // preserve the tree integrity, otherwise in release mode a RangeError will
  // be thrown and this EditableText will be built with a broken subtree.
  final composingRegionOutOfRange =
      !value.isComposingRangeValid || !withComposing;

  // Style when no cursor or selection is set
  if (composingRegionOutOfRange) {
    final textSpanChildren = utils.setEmojiTextStyle(
      text,
      emojiStyle: emojiTextStyle,
      parentStyle: style,
    );
    return TextSpan(style: style, children: textSpanChildren);
  }

  // Cursor will automatically highlight current word underlined
  final underlineStyle =
      style?.merge(const TextStyle(decoration: TextDecoration.underline)) ??
          const TextStyle(decoration: TextDecoration.underline);

  return TextSpan(
    style: style,
    children: <TextSpan>[
      TextSpan(
        children: utils.setEmojiTextStyle(
          value.composing.textBefore(value.text),
          emojiStyle: emojiTextStyle,
          parentStyle: style,
        ),
      ),
      TextSpan(
        children: utils.setEmojiTextStyle(
          value.composing.textInside(value.text),
          emojiStyle: emojiTextStyle,
          parentStyle: underlineStyle,
        ),
      ),
      TextSpan(
        children: utils.setEmojiTextStyle(
          value.composing.textAfter(value.text),
          emojiStyle: emojiTextStyle,
          parentStyle: style,
        ),
      ),
    ],
  );
}