Flutter文本解析插件efficient_parse_text的使用
Flutter文本解析插件efficient_parse_text的使用
细节文档
使用其实就是实现之前定义的抽象类接口。
1. 不匹配默认样式
这个很好理解,就是不匹配的文本展示样式,这个是必须实现的。
class HighlightUnMatch extends XHHighlightMatch {
[@override](/user/override)
InlineSpan matchBuilder(XHMatchInfo matchInfo) {
// 返回一个文本样式,默认为黑色字体
return TextSpan(
text: matchInfo.value,
style: const TextStyle(fontSize: 16, color: Colors.black));
}
[@override](/user/override)
Pattern? matchReg() {
// 不匹配任何模式
return null;
}
[@override](/user/override)
int get matchType => -1;
}
2. 匹配链接
class URLMatch extends XHHighlightMatch {
[@override](/user/override)
InlineSpan matchBuilder(XHMatchInfo matchInfo) {
// 返回一个点击手势,可以打开链接
return WidgetSpan(
alignment: PlaceholderAlignment.middle,
child: GestureDetector(
onTap: () {
print('to open url: ${matchInfo.value}');
},
child: Text(
matchInfo.value,
style: const TextStyle(
color: Colors.blue, fontSize: 16, fontWeight: FontWeight.bold),
)),
);
}
[@override](/user/override)
Pattern? matchReg() {
// 正则表达式用于匹配URL
return RegExp(
r"(http(s)?)://[a-zA-Z\d@:._+~#=-]{1,256}.[a-z\d]{2,18}\b([-a-zA-Z\d!@:_+.~#?&/%,$]*)(?<![$])");
}
[@override](/user/override)
int get matchType => 2;
}
3. 匹配邮箱地址
class EmailMatch extends XHHighlightMatch {
[@override](/user/override)
InlineSpan matchBuilder(XHMatchInfo matchInfo) {
// 返回一个带有粉色字体样式的文本
return (TextSpan(
text: matchInfo.value,
style: const TextStyle(color: Colors.pink, fontSize: 16)));
}
[@override](/user/override)
Pattern? matchReg() {
// 正则表达式用于匹配邮箱地址
return RegExp(r"\b[\w.-]+@[\w.-]+.\w{2,4}\b");
}
[@override](/user/override)
int get matchType => 3;
}
4. 匹配表情
class EmoMatch extends XHHighlightMatch {
// 这里可以添加更多表情的正则匹配逻辑
}
5. 初始化解析器
前面实现了3种匹配对象和1种不匹配对象, 如果你需要再匹配其他的类型,按照相同的方法添加就行了。是不是很简单!
final matchManager = XHHighlightMatchCore(matchList: [
EmailMatch(),
EmoMatch(),
URLMatch(),
], unMatch: HighlightUnMatch());
6. 创建组件
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
RichText(
text: TextSpan(
children: matchManager.matchThenGenInlineSpan(
"你好[爱心][呲牙] 看链接 https://mp.weixin.qq.com 邮箱地址: leaf@test.com;"))),
const Divider(),
RichText(
text: TextSpan(
children: matchManager.matchThenGenInlineSpan(
"你好[你好]https://www.baidu.com;llll"))),
const Divider(),
RichText(
text: TextSpan(
children: matchManager.matchThenGenInlineSpan("[你好]z[笑脸]l"))),
const Divider(),
RichText(
text: TextSpan(
children: matchManager.matchThenGenInlineSpan(
"Flutter调优工具使用及Flutter高性能编程部分要点分析:https://juejin.cn/post/7157905466821967902"))),
const Divider(),
RichText(
text:
TextSpan(children: matchManager.matchThenGenInlineSpan("L"))),
const Divider(),
RichText(
text: TextSpan(
children: matchManager.matchThenGenInlineSpan("leaf"))),
const Divider(),
],
),
);
}
}
更多关于Flutter文本解析插件efficient_parse_text的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复