我终于找到了符合我要求的要点。对于那些正在寻找我的问题的答案的人,您似乎必须覆盖 EditableText (如何动态更改文本字段中特定文本的颜色?)。但是,这只是一个草稿,目前还不能正常工作。我将尝试遵循这条路径并在这篇文章中添加我的答案。
编辑:
您唯一需要更改此答案的是以下内容:
@override
TextSpan buildTextSpan() {
final String text = textEditingValue.text;
int textLength = text.length;
if (widget.annotations != null && textLength > 0) {
var items = getRanges();
var children = <TextSpan>[];
for (var item in items) {
if (item.range.end < textLength) {
children.add(
TextSpan(style: item.style, text: item.range.textInside(text)),
);
} else if (item.range.start <= textLength) {
children.add(
TextSpan(
style: item.style,
text: TextRange(start: item.range.start, end: text.length)
.textInside(text)),
);
}
}
return new TextSpan(style: widget.style, children: children);
}
return new TextSpan(style: widget.style, text: text);
}
}
说明:
您只需更正 buildTextSpan 部分。删除字符时会引发错误,因为 Range 可能会在不满足范围结束时引发异常。