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

42 lines
1.1 KiB
Dart
Raw Normal View History

2022-09-17 14:35:45 +02:00
import 'package:flutter/widgets.dart';
2019-09-27 12:23:47 +02:00
import 'package:git_touch/utils/utils.dart';
2020-10-04 16:20:21 +02:00
class TextWithAt extends StatelessWidget {
2022-09-06 18:28:12 +02:00
const 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,
});
2022-09-21 18:28:21 +02:00
final String text;
final String Function(String text) linkFactory;
final TextStyle? style;
final bool oneLine;
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);
2022-09-24 07:41:46 +02:00
final spans = <TextSpan>[];
for (final 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),
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
}
}