?正则判断截取url地址,剩余的需要自己拼接
List<InlineSpan> _getContentSpan(String text) {
List<InlineSpan> _contentList = [];
RegExp exp =
new RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
// text = "如果www.baidu.com这是一段文本但是里面包含了连接";
Iterable<RegExpMatch> matches = exp.allMatches(text);
int index=0;
matches.forEach((match) {
/// start 0 end 8
/// start 10 end 12
String c = text.substring(match.start, match.end);
// print('---地址-url:--$c');
if(match.start==index){
index=match.end;
}
if(index<match.start){
String a=text.substring(index+1,match.start);
// print('---地址-内容AAAA--$a');
index=match.end;
_contentList.add(
TextSpan(text: a),
);
}
if (RegexUtil.isURL(c)) {
_contentList.add(TextSpan(
text: c,
style: TextStyle(color: Colors.blue),
recognizer: new TapGestureRecognizer()
..onTap = () {
Get.to(WebViewExample(
url: text.substring(match.start, match.end),
));
}));
} else {
_contentList.add(
TextSpan(text: c),
);
}
});
if(index<text.length){
String a=text.substring(index,text.length);
// print('---地址-内容BBBB--$a');
_contentList.add(
TextSpan(text: a),
);
}
return _contentList;
}
|