setEmojiTextStyle method
Produce a list of spans to adjust style for emoji characters.
Spans enclosing emojis will have parentStyle
combined with emojiStyle
.
Other spans will not have an explicit style (this method does not set
parentStyle
to the whole text.
Implementation
List<InlineSpan> setEmojiTextStyle(String text,
{required TextStyle emojiStyle, TextStyle? parentStyle}) {
final finalEmojiStyle =
parentStyle == null ? emojiStyle : parentStyle.merge(emojiStyle);
final matches = _emojiRegExp.allMatches(text).toList();
final spans = <InlineSpan>[];
var cursor = 0;
for (final match in matches) {
spans
..add(TextSpan(text: text.substring(cursor, match.start)))
..add(
TextSpan(
text: text.substring(match.start, match.end),
style: finalEmojiStyle,
),
);
cursor = match.end;
}
spans.add(TextSpan(text: text.substring(cursor, text.length)));
return spans;
}