使用extended_text_field組件進(jìn)行修改,
其中涉及一些組件RegExpSpecialText、RegExpSpecialTextSpanBuilder、SpecialTextSpan、ImageSpan、ExtendedWidgetSpan、ExtendedText.rich
SpecialTextSpan:
很關(guān)鍵的一個(gè)組件,能替換原有字符串的東西,顯示想要顯示的信息
SpecialTextSpan 核心功能
特殊文本標(biāo)記:可以標(biāo)識(shí)文本中的特定部分(如鏈接、話題標(biāo)簽等)
自定義手勢(shì)識(shí)別:為特殊文本添加點(diǎn)擊處理
光標(biāo)控制:正確處理特殊文本區(qū)域的光標(biāo)位置
文本替換:可以顯示與實(shí)際文本不同的內(nèi)容

image.png
SpecialTextSpan(
text: "點(diǎn)擊鏈接", // 顯示文本
actualText: "https://example.com", // 實(shí)際文本
start: 10, // 在原始文本中的起始位置
style: TextStyle(
color: Colors.blue,
decoration: TextDecoration.underline,
),
recognizer: TapGestureRecognizer()
..onTap = () {
print("鏈接被點(diǎn)擊: https://example.com");
// 可以調(diào)用 url_launcher 打開(kāi)鏈接
},
deleteAll: true, // 替換整個(gè)匹配文本
);
ExtendedTextField(
controller: _controller,
specialTextSpanBuilder: MySpecialTextBuilder(),
// 其他參數(shù)...
)
class MySpecialTextBuilder extends SpecialTextSpanBuilder {
@override
TextSpan build(
String data, {
TextStyle? textStyle,
SpecialTextSpanTapCallback? onTap,
}) {
// 實(shí)現(xiàn)特殊文本識(shí)別邏輯
final spans = <InlineSpan>[];
// 添加特殊文本
spans.add(SpecialTextSpan(
text: "[鏈接]",
actualText: "https://example.com",
start: 5,
style: textStyle?.copyWith(color: Colors.blue),
recognizer: TapGestureRecognizer()..onTap = () => onTap?.call("https://example.com"),
));
return TextSpan(children: spans);
}
}
其中遇到復(fù)雜問(wèn)題和靈活使用看情況處理,光標(biāo)問(wèn)題大部分都是start 和actualText的問(wèn)題