1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-24 06:42:07 +01:00
git-touch-android-ios-app/lib/widgets/text_contains_organization.dart
2020-01-26 23:44:45 +08:00

37 lines
1.0 KiB
Dart

import 'package:flutter/material.dart';
import 'package:git_touch/utils/utils.dart';
class TextContainsOrganization extends StatelessWidget {
final String text;
final TextStyle style;
final bool oneLine;
TextContainsOrganization(this.text, {this.style, this.oneLine = false});
static final _reg = RegExp(r'@[A-Za-z-]+');
@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]));
}
spans.add(
createLinkSpan(context, orgs[index], '/' + orgs[index].substring(1)));
}
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,
);
}
}