add custom link styles and fix bug in identifying link types

text with multiple linktypes enabled were not properly identified
This commit is contained in:
Abhijith K 2022-06-30 02:30:00 +05:30
parent 60010c07f9
commit 79be3afc55
4 changed files with 30 additions and 18 deletions

View File

@ -91,6 +91,12 @@ class _AppState extends State<App> {
textAlign: TextAlign.left,
linkTypes: texts[i]['types'],
textStyle: textStyle,
customLinkStyles: {
LinkType.email: TextStyle(color: Colors.blue),
LinkType.hashTag: TextStyle(color: Colors.green),
LinkType.userTag: TextStyle(color: Colors.deepPurple),
LinkType.url: TextStyle(color: Colors.pink),
},
linkStyle: textStyle.copyWith(
color: Colors.blue,
fontWeight: FontWeight.bold,

View File

@ -11,6 +11,7 @@ class LinkifyText extends StatelessWidget {
this.linkStyle,
this.linkTypes,
this.onTap,
this.customLinkStyles,
this.strutStyle,
this.textAlign,
this.textDirection,
@ -126,6 +127,8 @@ class LinkifyText extends StatelessWidget {
/// {@macro flutter.painting.textPainter.textWidthBasis}
final TextWidthBasis? textWidthBasis;
final Map<LinkType, TextStyle>? customLinkStyles;
@override
Widget build(BuildContext context) {
return Text.rich(
@ -134,7 +137,7 @@ class LinkifyText extends StatelessWidget {
linkStyle: linkStyle,
onTap: onTap,
linkTypes: linkTypes,
),
customLinkStyles: customLinkStyles),
key: key,
style: textStyle,
strutStyle: strutStyle,
@ -155,6 +158,7 @@ TextSpan _linkify({
String text = '',
TextStyle? linkStyle,
List<LinkType>? linkTypes,
Map<LinkType, TextStyle>? customLinkStyles,
Function(Link)? onTap,
}) {
final _regExp = constructRegExpFromLinkType(linkTypes ?? [LinkType.url]);
@ -177,7 +181,7 @@ TextSpan _linkify({
spans.add(
TextSpan(
text: link.value,
style: linkStyle,
style: customLinkStyles?[link.type] ?? linkStyle,
recognizer: TapGestureRecognizer()
..onTap = () {
if (onTap != null) onTap(link);

View File

@ -2,13 +2,15 @@ import 'package:linkfy_text/src/enum.dart';
import 'package:linkfy_text/src/utils/regex.dart';
class Link {
final String? _value;
final LinkType? _type;
late final String? _value;
late final LinkType? _type;
String? get value => _value;
LinkType? get type => _type;
/// construct link from matched regExp
Link.fromMatch(RegExpMatch match)
: _type = getMatchedType(match),
_value = match.input.substring(match.start, match.end);
Link.fromMatch(RegExpMatch match) {
String _match = match.input.substring(match.start, match.end);
_type = getMatchedType(_match);
_value = _match;
}
}

View File

@ -45,15 +45,15 @@ RegExp constructRegExpFromLinkType(List<LinkType> types) {
return RegExp(buffer.toString());
}
LinkType getMatchedType(RegExpMatch match) {
LinkType getMatchedType(String match) {
late LinkType type;
if (RegExp(urlRegExp).hasMatch(match.input)) {
type = LinkType.url;
} else if (RegExp(hashtagRegExp).hasMatch(match.input)) {
type = LinkType.hashTag;
} else if (RegExp(emailRegExp).hasMatch(match.input)) {
if (RegExp(emailRegExp).hasMatch(match)) {
type = LinkType.email;
} else if (RegExp(userTagRegExp).hasMatch(match.input)) {
} else if (RegExp(urlRegExp).hasMatch(match)) {
type = LinkType.url;
} else if (RegExp(hashtagRegExp).hasMatch(match)) {
type = LinkType.hashTag;
} else if (RegExp(userTagRegExp).hasMatch(match)) {
type = LinkType.userTag;
}
return type;