-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathspecial_text_span_builder.dart
203 lines (178 loc) · 5.38 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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) {
// always append
// and remove endflag in getContent method
specialText.appendContent(char);
if (specialText.isEnd(textStack)) {
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) => value.endsWith(endFlag);
/// append text of SpecialText
void appendContent(String value) {
_content.write(value);
}
/// get content of SpecialText(not include startFlag and endFlag)
/// https://github.com/fluttercandies/extended_text/issues/76
String getContent() {
String content = _content.toString();
if (content.endsWith(endFlag)) {
content = content.substring(
0,
content.length - endFlag.length,
);
}
return content;
}
@override
String toString() {
return startFlag + getContent() + endFlag;
}
}
abstract class RegExpSpecialTextSpanBuilder extends SpecialTextSpanBuilder {
List<RegExpSpecialText> get regExps;
@override
TextSpan build(String data,
{TextStyle? textStyle, SpecialTextGestureTapCallback? onTap}) {
final List<InlineSpan> children = <InlineSpan>[];
if (regExps.isNotEmpty) {
buildWithRegExp(
children: children,
start: 0,
data: data,
copyRegExps: regExps.toList(),
textStyle: textStyle,
onTap: onTap,
);
}
return TextSpan(children: children, style: textStyle);
}
void buildWithRegExp({
required String data,
required int start,
required List<RegExpSpecialText> copyRegExps,
required List<InlineSpan> children,
TextStyle? textStyle,
SpecialTextGestureTapCallback? onTap,
}) {
if (data.isEmpty) {
return;
}
if (copyRegExps.isEmpty) {
children.add(TextSpan(text: data, style: textStyle));
return;
}
final RegExpSpecialText regExpSpecialText = copyRegExps.first;
data.splitMapJoin(regExpSpecialText.regExp, onMatch: (Match match) {
final String matchString = '${match[0]}';
children.add(regExpSpecialText.finishText(
start,
match,
textStyle: textStyle,
onTap: onTap,
));
start += matchString.length;
return '';
}, onNonMatch: (String notMatch) {
if (notMatch.isNotEmpty) {
buildWithRegExp(
data: notMatch,
start: start,
children: children,
copyRegExps: copyRegExps.toList()..remove(regExpSpecialText),
textStyle: textStyle,
onTap: onTap,
);
start += notMatch.length;
}
return '';
});
}
@override
SpecialText? createSpecialText(String flag,
{TextStyle? textStyle,
SpecialTextGestureTapCallback? onTap,
required int index}) {
return null;
}
}
abstract class RegExpSpecialText {
RegExp get regExp;
InlineSpan finishText(
int start,
Match match, {
TextStyle? textStyle,
SpecialTextGestureTapCallback? onTap,
});
}