fix(gh): text with at notation

This commit is contained in:
Rongjian Zhang 2020-10-04 22:20:21 +08:00
parent 1fe7e1fdc9
commit 2248415574
2 changed files with 15 additions and 8 deletions

View File

@ -10,7 +10,7 @@ import 'package:git_touch/widgets/mutation_button.dart';
import 'package:git_touch/widgets/entry_item.dart';
import 'package:git_touch/widgets/repository_item.dart';
import 'package:git_touch/widgets/table_view.dart';
import 'package:git_touch/widgets/text_contains_organization.dart';
import 'package:git_touch/widgets/text_with_at.dart';
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/widgets/user_header.dart';
import 'package:provider/provider.dart';
@ -166,8 +166,9 @@ class GhUserScreen extends StatelessWidget {
if (isNotNullOrEmpty(p.company))
TableViewItem(
leftIconData: Octicons.organization,
text: TextContainsOrganization(
p.company,
text: TextWithAt(
text: p.company,
linkFactory: (text) => '/github/' + text.substring(1),
style: TextStyle(fontSize: 17, color: theme.palette.text),
oneLine: true,
),

View File

@ -1,27 +1,33 @@
import 'package:flutter/material.dart';
import 'package:git_touch/utils/utils.dart';
class TextContainsOrganization extends StatelessWidget {
class TextWithAt extends StatelessWidget {
final String text;
final String Function(String text) linkFactory;
final TextStyle style;
final bool oneLine;
TextContainsOrganization(this.text, {this.style, this.oneLine = false});
TextWithAt({
@required this.text,
@required this.linkFactory,
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 matches = _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))) {
for (var index in List.generate(matches.length, (i) => (i))) {
if (chunks[index].isNotEmpty) {
spans.add(TextSpan(text: chunks[index]));
}
spans.add(
createLinkSpan(context, orgs[index], '/' + orgs[index].substring(1)));
createLinkSpan(context, matches[index], linkFactory(matches[index])));
}
if (chunks.last.isNotEmpty) {
spans.add(TextSpan(text: chunks.last));