-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathtext_with_at.dart
41 lines (36 loc) · 1.14 KB
/
text_with_at.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
import 'package:flutter/widgets.dart';
import 'package:git_touch/utils/utils.dart';
class TextWithAt extends StatelessWidget {
const TextWithAt({
required this.text,
required this.linkFactory,
this.style,
this.oneLine = false,
});
final String text;
final String Function(String text) linkFactory;
final TextStyle? style;
final bool oneLine;
static final _reg = RegExp(r'@[A-Za-z-]+');
@override
Widget build(BuildContext context) {
final matches = _reg.allMatches(text).map((m) => m.group(0)).toList();
final chunks = text.split(_reg);
final spans = <TextSpan>[];
for (final index in List.generate(matches.length, (i) => (i))) {
if (chunks[index].isNotEmpty) {
spans.add(TextSpan(text: chunks[index]));
}
spans.add(createLinkSpan(
context, matches[index], linkFactory(matches[index] ?? '')));
}
if (chunks.last.isNotEmpty) {
spans.add(TextSpan(text: chunks.last));
}
return Text.rich(
TextSpan(children: spans, style: style),
overflow: oneLine ? TextOverflow.ellipsis : TextOverflow.clip,
maxLines: oneLine ? 1 : null,
);
}
}