< img alt="пример duolingo" src="

На данный момент у меня есть AnnotatedString, которая может напечатать слово при нажатии. Вот как я генерирую эту AnnotatedString на основе входной строки:
Код: Выделить всё
val input = // get some string input from user
val words = input.parseInput() // split a String into a list of words and spaces (e.g. "Hello world" -> ["Hello", " ", "world"])
data class AnnotatedWord(word: String, tag: String)
val annotatedWords = emptyList()
var wordIndex = 0
val annotatedString = buildAnnotatedString {
words.forEach { word ->
if (word == " ") {
append(word)
continue
}
val tag = "${word}:${wordIndex++}"
pushStringAnnotation(tag = tag, annotation = tag)
withStyle(style = SpanStyle(textDecoration = TextDecoration.Underline)) {
append(word)
}
pop()
annotatedWords.add(AnnotatedWord(word, tag))
}
}
Код: Выделить всё
ClickableText(
text = annotatedString,
onClick = { index ->
val annotation = annotatedString.getStringAnnotations(start = index, end = index).firstOrNull()
if (annotation != null) {
val tag = annotation.tag
val matchingWord = annotatedWords.find { it.tag == tag }
print(matchingWord.word)
}
}
)
Подробнее здесь: https://stackoverflow.com/questions/783 ... e-duolingo