parseOracleTextString function
Parses an OracleText string and returns a TextSpan with embedded mana symbols and other MTG symbols.
Example:
TextSpan? span = parseOracleTextString('Add {W} or {U}, then {T} to untap.');
Returns null
if the input string is null
.
Implementation
TextSpan? parseOracleTextString(String? oracleText) {
if (oracleText == null) {
return null;
}
Iterable<RegExpMatch> matches = MTGSymbol.regex.allMatches(oracleText);
if (matches.isEmpty) {
return TextSpan(text: oracleText);
}
final List<InlineSpan> children = [];
int lastIndex = 0;
for (RegExpMatch match in matches) {
children.add(TextSpan(text: oracleText.substring(lastIndex, match.start)));
String? matchedSymbol = match.group(0);
final MTGSymbol? mtgSymbol = mtgSymbology[matchedSymbol];
if (mtgSymbol == null) {
throw ArgumentError.value(
matchedSymbol,
'matchedSymbol',
'Unexpected MTG symbol',
);
}
children.add(
WidgetSpan(child: mtgSymbol.toSvg(height: 14.0)),
); // Smaller for inline text
lastIndex = match.end;
}
children.add(TextSpan(text: oracleText.substring(lastIndex)));
return TextSpan(children: children);
}