git-touch-android-ios-app/lib/widgets/text_with_at.dart

43 lines
1.1 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';
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;
2019-09-27 12:23:47 +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({
@required this.text,
@required this.linkFactory,
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]));
}
2019-12-12 13:29:56 +01:00
spans.add(
2020-10-04 16:20:21 +02:00
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),
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
}
}