如何在visbilitydetector内部使用TextSpan作为Widget



我的应用程序中有文本,我想随着时间的推移不同的颜色。我通过使用RichText小部件为每个字符提供单独的文本跨度和计时器来实现这一点,计时器将更新状态并适当地重新绘制所有文本跨度。只要文本不太长,这种方法就有效。它在大约7-10k字符的文本中开始中断。

为了优化这一点,我决定使用visbilitydetector库,因为不可见的文本不需要不同的颜色。因此,我将文本分块,并将每个块放在自己的可见性检测器中,当它不可见时,我只需使用text小部件设置文本。这是可行的,但由于它们是独立的小部件,因此单行会在中途被切断并从下一行开始。

我想做的是传递TextSpan作为可视性检测器的子,但这给出了错误,TextSpan is not a subtype of the type Widget。有什么办法能做到我想做的吗?

这是我想要的小部件树的类型:

String myText = '';
RichText(
text: TextSpan(
children: myText.chunk().mapIndexed((chunkIndex, chunkText) {
return WidgetSpan(
child: VisibilityDetector(
onVisibilityChanged: (info) => _handleVisibilityChanged(),
child: !chunkIsVisible ? 
Text(chunkText) :
TextSpan( //This breaks because its not a subtype of Widget
children: chunkText.characters.mapIndexed((charIndex, char) {
return TextSpan(
text: char,
style: _styleTextBasedOnIndex((chunkIndex * ChunkSize) + charIndex)
)
}
)
)
)
}
)
)

我认为你可以这样做来传递错误:

String myText = '';
RichText(
text: TextSpan(
children: myText.chunk().mapIndexed((chunkIndex, chunkText) {
return WidgetSpan(
child: VisibilityDetector(
onVisibilityChanged: (info) => _handleVisibilityChanged(),
child: !chunkIsVisible ? 
Text(chunkText) :
RichText(text: TextSpan( // Use another RichText
children: chunkText.characters.mapIndexed((charIndex, char) {
return TextSpan(
text: char,
style: _styleTextBasedOnIndex((chunkIndex * ChunkSize) + charIndex)
)
}
)
)
)
)
}
)
)

最新更新