今天繼續講講怎麼樣快速構建特殊文字以及自定義背景,我喜歡那個圖,再次送上git
你們刷微博應該都常常看到,文字裏面有一些好比@某某某,或者一些話題的連接。可是服務器端通常都是給你一段文字,你須要本身去作匹配。之前可能每加個邏輯,你就要寫一段代碼,可是有了Extended text,一切都變得so easy。 github
舉個栗子,好比咱們要匹配@某某某,咱們只須要繼承SpecialText,而且定義它的開始標誌和結束標誌服務器
class AtText extends SpecialText {
static const String flag = "@";
AtText(TextStyle textStyle, SpecialTextGestureTapCallback onTap)
: super(flag, " ", textStyle, onTap: onTap);
@override
TextSpan finishText() {
// TODO: implement finishText
final String atText = toString();
return TextSpan(
text: atText,
style: textStyle?.copyWith(color: Colors.blue, fontSize: 16.0),
recognizer: TapGestureRecognizer()
..onTap = () {
if (onTap != null) onTap(atText);
});
}
}
複製代碼
而後,咱們須要定義匹配規則,繼承SpecialTextSpanBuilder,實現方法app
class MySpecialTextSpanBuilder extends SpecialTextSpanBuilder {
@override
TextSpan build(String data,
{TextStyle textStyle, SpecialTextGestureTapCallback onTap}) {
if (data == null || data == "") return null;
List<TextSpan> inlineList = new List<TextSpan>();
if (data != null && data.length > 0) {
SpecialText specialText;
String textStack = "";
//String text
for (int i = 0; i < data.length; i++) {
String char = data[i];
if (specialText != null) {
if (!specialText.isEnd(char)) {
specialText.appendContent(char);
} else {
inlineList.add(specialText.finishText());
specialText = null;
}
} else {
textStack += char;
specialText =
createSpecialText(textStack, textStyle: textStyle, onTap: onTap);
if (specialText != null) {
if (textStack.length - specialText.startFlag.length >= 0) {
textStack = textStack.substring(
0, textStack.length - specialText.startFlag.length);
if (textStack.length > 0) {
inlineList.add(TextSpan(text: textStack, style: textStyle));
}
}
textStack = "";
}
}
}
if (specialText != null) {
inlineList.add(TextSpan(
text: specialText.startFlag + specialText.getContent(),
style: textStyle));
} else if (textStack.length > 0) {
inlineList.add(TextSpan(text: textStack, style: textStyle));
}
}
// TODO: implement build
return TextSpan(children: inlineList, style: textStyle);
}
@override
SpecialText createSpecialText(String flag,
{TextStyle textStyle, SpecialTextGestureTapCallback onTap}) {
if (flag == null || flag == "") return null;
// TODO: implement createSpecialText
if (isStart(flag, AtText.flag)) {
return AtText(textStyle, onTap);
} else if (isStart(flag, EmojiText.flag)) {
return EmojiText(textStyle);
} else if (isStart(flag, DollarText.flag)) {
return DollarText(textStyle, onTap);
}
return null;
}
}
複製代碼
最後使用的時候是這樣子的ide
ExtendedText(
"[love]Extended text help you to build rich text quickly. any special text you will have with extended text. "
"\n\nIt's my pleasure to invite you to join \$FlutterCandies\$ if you want to improve flutter .[love]"
"\n\nif you meet any problem, please let me konw @zmtzawqlp .[sun_glasses]",
onSpecialTextTap: (String data) {
if (data.startsWith("\$")) {
launch("https://github.com/fluttercandies");
} else if (data.startsWith("@")) {
launch("mailto:zmtzawqlp@live.com");
}
},
specialTextSpanBuilder: MySpecialTextSpanBuilder(),
overflow: TextOverflow.ellipsis,
//style: TextStyle(background: Paint()..color = Colors.red),
maxLines: 10,
),
複製代碼
是否是以爲特別簡單,並且可擴展性強,媽媽不再會擔憂我無法作特殊文字的匹配了。。 上個效果圖哈svg
最後放上 Github Extended_Text,若是你有什麼不明白的地方,請告訴我,歡迎加入Flutter Candies,一塊兒生產可愛的Flutter 小糖果(QQ羣:181398081)post