buildEmphasizedTextWidgets function
List<TextSpan>
buildEmphasizedTextWidgets({
- required String text,
- required Iterable<
WordMarker> markers, - required TextStyle textStyle,
- required TextStyle wordStyle,
Returns a list of widgets representing texts parts, each with its own style.
Text enclosed by start and end markers, gets the given wordStyle.
Text not enclosed by markers, gets the given textStyle.
Implementation
List<TextSpan> buildEmphasizedTextWidgets({
required final String text,
required Iterable<WordMarker> markers,
required TextStyle textStyle,
required TextStyle wordStyle,
}) {
int runningIndex = 0;
List<TextSpan> emphasizedTextWidgets = <TextSpan>[];
for (WordMarker marker in markers) {
final String substring = text.substring(runningIndex, marker.index);
if (marker.type == MarkerType.start) {
emphasizedTextWidgets.add(TextSpan(text: substring, style: textStyle));
} else if (marker.type == MarkerType.end) {
emphasizedTextWidgets.add(TextSpan(text: substring, style: wordStyle));
}
runningIndex = marker.index;
}
// add last part of text
final String substring = text.substring(runningIndex, text.length);
emphasizedTextWidgets.add(TextSpan(text: substring, style: textStyle));
return emphasizedTextWidgets;
}