From 79be3afc553b786e6a87c1441d2175f742765995 Mon Sep 17 00:00:00 2001 From: Abhijith K Date: Thu, 30 Jun 2022 02:30:00 +0530 Subject: [PATCH] add custom link styles and fix bug in identifying link types text with multiple linktypes enabled were not properly identified --- example/lib/main.dart | 6 ++++++ lib/src/linkify.dart | 16 ++++++++++------ lib/src/model/link.dart | 12 +++++++----- lib/src/utils/regex.dart | 14 +++++++------- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 691e88a..ef6d549 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -91,6 +91,12 @@ class _AppState extends State { 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, diff --git a/lib/src/linkify.dart b/lib/src/linkify.dart index 7668295..6ddea2d 100644 --- a/lib/src/linkify.dart +++ b/lib/src/linkify.dart @@ -11,6 +11,7 @@ class LinkifyText extends StatelessWidget { this.linkStyle, this.linkTypes, this.onTap, + this.customLinkStyles, this.strutStyle, this.textAlign, this.textDirection, @@ -126,15 +127,17 @@ class LinkifyText extends StatelessWidget { /// {@macro flutter.painting.textPainter.textWidthBasis} final TextWidthBasis? textWidthBasis; + final Map? customLinkStyles; + @override Widget build(BuildContext context) { return Text.rich( _linkify( - text: text, - linkStyle: linkStyle, - onTap: onTap, - linkTypes: linkTypes, - ), + text: text, + 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? linkTypes, + Map? 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); diff --git a/lib/src/model/link.dart b/lib/src/model/link.dart index 8136401..1679af7 100644 --- a/lib/src/model/link.dart +++ b/lib/src/model/link.dart @@ -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; + } } diff --git a/lib/src/utils/regex.dart b/lib/src/utils/regex.dart index 0640fd2..2a4a4e5 100644 --- a/lib/src/utils/regex.dart +++ b/lib/src/utils/regex.dart @@ -45,15 +45,15 @@ RegExp constructRegExpFromLinkType(List 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;