1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-20 20:13:55 +01:00
git-touch-android-ios-app/lib/widgets/text_contains_organization.dart

37 lines
1.0 KiB
Dart
Raw Normal View History

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));
}
2020-01-26 16:44:45 +01:00
return Text.rich(
TextSpan(children: spans, style: style),
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
}
}