-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathspecial_text_span_builder.dart
110 lines (95 loc) · 2.99 KB
/
special_text_span_builder.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import 'package:extended_text_library/src/extended_text_typedef.dart';
import 'package:flutter/material.dart';
abstract class SpecialTextSpanBuilder {
//build text span to specialText
TextSpan build(String data,
{TextStyle? textStyle, SpecialTextGestureTapCallback? onTap}) {
if (data == '') {
return const TextSpan(text: '');
}
final List<InlineSpan> inlineList = <InlineSpan>[];
if (data.isNotEmpty) {
SpecialText? specialText;
String textStack = '';
//String text
for (int i = 0; i < data.length; i++) {
final String char = data[i];
textStack += char;
if (specialText != null) {
if (!specialText.isEnd(textStack)) {
specialText.appendContent(char);
} else {
inlineList.add(specialText.finishText());
specialText = null;
textStack = '';
}
} else {
specialText = createSpecialText(textStack,
textStyle: textStyle, onTap: onTap, index: i);
if (specialText != null) {
if (textStack.length - specialText.startFlag.length >= 0) {
textStack = textStack.substring(
0, textStack.length - specialText.startFlag.length);
if (textStack.isNotEmpty) {
inlineList.add(TextSpan(text: textStack, style: textStyle));
}
}
textStack = '';
}
}
}
if (specialText != null) {
inlineList.add(TextSpan(
text: specialText.startFlag + specialText.getContent(),
style: textStyle));
} else if (textStack.isNotEmpty) {
inlineList.add(TextSpan(text: textStack, style: textStyle));
}
} else {
inlineList.add(TextSpan(text: data, style: textStyle));
}
return TextSpan(children: inlineList, style: textStyle);
}
//build SpecialText base on startflag
SpecialText? createSpecialText(
String flag, {
TextStyle? textStyle,
SpecialTextGestureTapCallback? onTap,
required int index,
});
/// start with SpecialText
bool isStart(String value, String startFlag) {
return value.endsWith(startFlag);
}
}
abstract class SpecialText {
SpecialText(this.startFlag, this.endFlag, this.textStyle, {this.onTap})
: _content = StringBuffer();
final StringBuffer _content;
///start flag of SpecialText
final String startFlag;
///end flag of SpecialText
final String endFlag;
///TextStyle of SpecialText
final TextStyle textStyle;
///tap call back of SpecialText
final SpecialTextGestureTapCallback? onTap;
///finish SpecialText
InlineSpan finishText();
///is end of SpecialText
bool isEnd(String value) {
return value.endsWith(endFlag);
}
///append text of SpecialText
void appendContent(String value) {
_content.write(value);
}
///get content of SpecialText
String getContent() {
return _content.toString();
}
@override
String toString() {
return startFlag + getContent() + endFlag;
}
}