2019-09-27 12:23:47 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:git_touch/utils/utils.dart';
|
|
|
|
|
|
|
|
class TextContainsOrganization extends StatelessWidget {
|
|
|
|
final String text;
|
|
|
|
final TextStyle style;
|
2020-01-01 05:55:27 +01:00
|
|
|
final bool oneLine;
|
2019-09-27 12:23:47 +02:00
|
|
|
|
2020-01-01 05:55:27 +01:00
|
|
|
TextContainsOrganization(this.text, {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) {
|
|
|
|
final orgs = _reg.allMatches(text).map((m) => m.group(0)).toList();
|
|
|
|
final chunks = text.split(_reg);
|
|
|
|
|
|
|
|
List<TextSpan> spans = [];
|
|
|
|
for (var index in List.generate(orgs.length, (i) => (i))) {
|
|
|
|
if (chunks[index].isNotEmpty) {
|
|
|
|
spans.add(TextSpan(text: chunks[index]));
|
|
|
|
}
|
2019-12-12 13:29:56 +01:00
|
|
|
spans.add(
|
|
|
|
createLinkSpan(context, orgs[index], '/' + orgs[index].substring(1)));
|
2019-09-27 12:23:47 +02:00
|
|
|
}
|
|
|
|
if (chunks.last.isNotEmpty) {
|
|
|
|
spans.add(TextSpan(text: chunks.last));
|
|
|
|
}
|
|
|
|
|
|
|
|
return RichText(
|
2020-01-01 05:55:27 +01:00
|
|
|
text: 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
|
|
|
}
|
|
|
|
}
|