forked from jonataslaw/readmore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadmore.dart
354 lines (312 loc) · 10.6 KB
/
readmore.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
library readmore;
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
enum TrimMode {
Length,
Line,
}
class ReadMoreText extends StatefulWidget {
const ReadMoreText(this.data,
{Key? key,
this.preDataText,
this.postDataText,
this.preDataTextStyle,
this.postDataTextStyle,
this.trimExpandedText = 'show less',
this.trimCollapsedText = 'read more',
this.colorClickableText,
this.trimLength = 240,
this.trimLines = 2,
this.trimMode = TrimMode.Length,
this.style,
this.textAlign,
this.textDirection,
this.locale,
this.textScaleFactor,
this.semanticsLabel,
this.moreStyle,
this.lessStyle,
this.delimiter = _kEllipsis + ' ',
this.delimiterStyle,
this.callback,
this.onLinkPressed,
this.linkTextStyle,
this.enableTap = true})
: super(key: key);
/// Used on TrimMode.Length
final int trimLength;
/// Used on TrimMode.Lines
final int trimLines;
/// Determines the type of trim. TrimMode.Length takes into account
/// the number of letters, while TrimMode.Lines takes into account
/// the number of lines
final TrimMode trimMode;
/// TextStyle for expanded text
final TextStyle? moreStyle;
/// TextStyle for compressed text
final TextStyle? lessStyle;
/// Textspan used before the data any heading or somthing
final String? preDataText;
/// Textspan used after the data end or before the more/less
final String? postDataText;
/// Textspan used before the data any heading or somthing
final TextStyle? preDataTextStyle;
/// Textspan used after the data end or before the more/less
final TextStyle? postDataTextStyle;
///Called when state change between expanded/compress
final Function(bool val)? callback;
///Enable onTap
final bool enableTap;
final ValueChanged<String>? onLinkPressed;
final TextStyle? linkTextStyle;
final String delimiter;
final String data;
final String trimExpandedText;
final String trimCollapsedText;
final Color? colorClickableText;
final TextStyle? style;
final TextAlign? textAlign;
final TextDirection? textDirection;
final Locale? locale;
final double? textScaleFactor;
final String? semanticsLabel;
final TextStyle? delimiterStyle;
@override
ReadMoreTextState createState() => ReadMoreTextState();
}
const String _kEllipsis = '\u2026';
const String _kLineSeparator = '\u2028';
class ReadMoreTextState extends State<ReadMoreText> {
bool _readMore = true;
void _onTapLink() {
setState(() {
_readMore = !_readMore;
widget.callback?.call(_readMore);
});
}
@override
Widget build(BuildContext context) {
final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
TextStyle? effectiveTextStyle = widget.style;
if (widget.style?.inherit ?? false) {
effectiveTextStyle = defaultTextStyle.style.merge(widget.style);
}
final textAlign =
widget.textAlign ?? defaultTextStyle.textAlign ?? TextAlign.start;
final textDirection = widget.textDirection ?? Directionality.of(context);
final textScaleFactor =
widget.textScaleFactor ?? MediaQuery.textScaleFactorOf(context);
final overflow = defaultTextStyle.overflow;
final locale = widget.locale ?? Localizations.maybeLocaleOf(context);
final colorClickableText =
widget.colorClickableText ?? Theme.of(context).colorScheme.secondary;
final _defaultLessStyle = widget.lessStyle ??
effectiveTextStyle?.copyWith(color: colorClickableText);
final _defaultMoreStyle = widget.moreStyle ??
effectiveTextStyle?.copyWith(color: colorClickableText);
final _defaultDelimiterStyle = widget.delimiterStyle ?? effectiveTextStyle;
TextSpan link = TextSpan(
text: _readMore ? widget.trimCollapsedText : widget.trimExpandedText,
style: _readMore ? _defaultMoreStyle : _defaultLessStyle,
recognizer: widget.enableTap
? (TapGestureRecognizer()..onTap = _onTapLink)
: null,
);
TextSpan _delimiter = TextSpan(
text: _readMore
? widget.trimCollapsedText.isNotEmpty
? widget.delimiter
: ''
: '',
style: _defaultDelimiterStyle,
recognizer: widget.enableTap
? (TapGestureRecognizer()..onTap = _onTapLink)
: null,
);
Widget result = LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
assert(constraints.hasBoundedWidth);
final double maxWidth = constraints.maxWidth;
TextSpan? preTextSpan;
TextSpan? postTextSpan;
if (widget.preDataText != null)
preTextSpan = TextSpan(
text: widget.preDataText! + " ",
style: widget.preDataTextStyle ?? effectiveTextStyle,
);
if (widget.postDataText != null)
postTextSpan = TextSpan(
text: " " + widget.postDataText!,
style: widget.postDataTextStyle ?? effectiveTextStyle,
);
// Create a TextSpan with data
final text = TextSpan(
children: [
if (preTextSpan != null) preTextSpan,
TextSpan(text: widget.data, style: effectiveTextStyle),
if (postTextSpan != null) postTextSpan
],
);
// Layout and measure link
TextPainter textPainter = TextPainter(
text: link,
textAlign: textAlign,
textDirection: textDirection,
textScaleFactor: textScaleFactor,
maxLines: widget.trimLines,
ellipsis: overflow == TextOverflow.ellipsis ? widget.delimiter : null,
locale: locale,
);
textPainter.layout(minWidth: 0, maxWidth: maxWidth);
final linkSize = textPainter.size;
// Layout and measure delimiter
textPainter.text = _delimiter;
textPainter.layout(minWidth: 0, maxWidth: maxWidth);
final delimiterSize = textPainter.size;
// Layout and measure text
textPainter.text = text;
textPainter.layout(minWidth: constraints.minWidth, maxWidth: maxWidth);
final textSize = textPainter.size;
// Get the endIndex of data
bool linkLongerThanLine = false;
int endIndex;
if (linkSize.width < maxWidth) {
final readMoreSize = linkSize.width + delimiterSize.width;
final pos = textPainter.getPositionForOffset(Offset(
textDirection == TextDirection.rtl
? readMoreSize
: textSize.width - readMoreSize,
textSize.height,
));
endIndex = textPainter.getOffsetBefore(pos.offset) ?? 0;
} else {
var pos = textPainter.getPositionForOffset(
textSize.bottomLeft(Offset.zero),
);
endIndex = pos.offset;
linkLongerThanLine = true;
}
var textSpan;
switch (widget.trimMode) {
case TrimMode.Length:
if (widget.trimLength < widget.data.length) {
textSpan = _buildData(
data: _readMore
? widget.data.substring(0, widget.trimLength)
: widget.data,
textStyle: effectiveTextStyle,
linkTextStyle: effectiveTextStyle?.copyWith(
decoration: TextDecoration.underline,
color: Colors.blue,
),
onPressed: widget.onLinkPressed,
children: [_delimiter, link],
);
} else {
textSpan = _buildData(
data: widget.data,
textStyle: effectiveTextStyle,
linkTextStyle: effectiveTextStyle?.copyWith(
decoration: TextDecoration.underline,
color: Colors.blue,
),
onPressed: widget.onLinkPressed,
children: [],
);
}
break;
case TrimMode.Line:
if (textPainter.didExceedMaxLines) {
textSpan = _buildData(
data: _readMore
? widget.data.substring(0, endIndex) +
(linkLongerThanLine ? _kLineSeparator : '')
: widget.data,
textStyle: effectiveTextStyle,
linkTextStyle: effectiveTextStyle?.copyWith(
decoration: TextDecoration.underline,
color: Colors.blue,
),
onPressed: widget.onLinkPressed,
children: [_delimiter, link],
);
} else {
textSpan = _buildData(
data: widget.data,
textStyle: effectiveTextStyle,
linkTextStyle: effectiveTextStyle?.copyWith(
decoration: TextDecoration.underline,
color: Colors.blue,
),
onPressed: widget.onLinkPressed,
children: [],
);
}
break;
default:
throw Exception(
'TrimMode type: ${widget.trimMode} is not supported');
}
return Text.rich(
TextSpan(
children: [
if (preTextSpan != null) preTextSpan,
textSpan,
if (postTextSpan != null) postTextSpan,
],
),
textAlign: textAlign,
textDirection: textDirection,
softWrap: true,
overflow: TextOverflow.clip,
textScaleFactor: textScaleFactor,
);
},
);
if (widget.semanticsLabel != null) {
result = Semantics(
textDirection: widget.textDirection,
label: widget.semanticsLabel,
child: ExcludeSemantics(
child: result,
),
);
}
return result;
}
TextSpan _buildData({
required String data,
TextStyle? textStyle,
TextStyle? linkTextStyle,
ValueChanged<String>? onPressed,
required List<TextSpan> children,
}) {
RegExp exp = RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
List<TextSpan> contents = [];
while (exp.hasMatch(data)) {
final match = exp.firstMatch(data);
final firstTextPart = data.substring(0, match!.start);
final linkTextPart = data.substring(match.start, match.end);
contents.add(
TextSpan(
text: firstTextPart,
),
);
contents.add(
TextSpan(
text: linkTextPart,
),
);
data = data.substring(match.end, data.length);
}
contents.add(
TextSpan(
text: data,
),
);
return TextSpan(
children: contents..addAll(children),
style: textStyle,
);
}
}