2019-09-27 12:23:47 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:git_touch/utils/utils.dart';
|
|
|
|
|
2020-10-04 16:20:21 +02:00
|
|
|
class TextWithAt extends StatelessWidget {
|
2019-09-27 12:23:47 +02:00
|
|
|
final String text;
|
2020-10-04 16:20:21 +02:00
|
|
|
final String Function(String text) linkFactory;
|
2021-05-16 09:16:35 +02:00
|
|
|
final TextStyle? style;
|
2020-01-01 05:55:27 +01:00
|
|
|
final bool oneLine;
|
2019-09-27 12:23:47 +02:00
|
|
|
|
2020-10-04 16:20:21 +02:00
|
|
|
TextWithAt({
|
2021-05-16 09:16:35 +02:00
|
|
|
required this.text,
|
|
|
|
required this.linkFactory,
|
2020-10-04 16:20:21 +02:00
|
|
|
this.style,
|
|
|
|
this.oneLine = false,
|
|
|
|
});
|
2019-09-27 12:23:47 +02:00
|
|
|
|
2019-11-02 12:44:24 +01:00
|
|
|
static final _reg = RegExp(r'@[A-Za-z-]+');
|
2019-09-27 12:23:47 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-10-04 16:20:21 +02:00
|
|
|
final matches = _reg.allMatches(text).map((m) => m.group(0)).toList();
|
2019-09-27 12:23:47 +02:00
|
|
|
final chunks = text.split(_reg);
|
|
|
|
|
|
|
|
List<TextSpan> spans = [];
|
2020-10-04 16:20:21 +02:00
|
|
|
for (var index in List.generate(matches.length, (i) => (i))) {
|
2019-09-27 12:23:47 +02:00
|
|
|
if (chunks[index].isNotEmpty) {
|
|
|
|
spans.add(TextSpan(text: chunks[index]));
|
|
|
|
}
|
2021-05-16 09:16:35 +02:00
|
|
|
spans.add(createLinkSpan(
|
|
|
|
context, matches[index], linkFactory(matches[index] ?? '')));
|
2019-09-27 12:23:47 +02:00
|
|
|
}
|
|
|
|
if (chunks.last.isNotEmpty) {
|
|
|
|
spans.add(TextSpan(text: chunks.last));
|
|
|
|
}
|
|
|
|
|
2020-01-26 16:44:45 +01:00
|
|
|
return Text.rich(
|
|
|
|
TextSpan(children: spans, style: style),
|
2020-01-01 07:09:26 +01:00
|
|
|
overflow: oneLine ? TextOverflow.ellipsis : TextOverflow.clip,
|
2020-01-01 05:55:27 +01:00
|
|
|
maxLines: oneLine ? 1 : null,
|
|
|
|
);
|
2019-09-27 12:23:47 +02:00
|
|
|
}
|
|
|
|
}
|