Merge pull request #6 from AbhijithKonnayil/5-custom-link-stlye

add custom link styles and fix bug in identifying link types
This commit is contained in:
Stanley Akpama 2022-07-01 01:29:41 +01:00 committed by GitHub
commit 8da1999a72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 18 deletions

View File

@ -91,6 +91,12 @@ class _AppState extends State<App> {
textAlign: TextAlign.left, textAlign: TextAlign.left,
linkTypes: texts[i]['types'], linkTypes: texts[i]['types'],
textStyle: textStyle, 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( linkStyle: textStyle.copyWith(
color: Colors.blue, color: Colors.blue,
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,

View File

@ -11,6 +11,7 @@ class LinkifyText extends StatelessWidget {
this.linkStyle, this.linkStyle,
this.linkTypes, this.linkTypes,
this.onTap, this.onTap,
this.customLinkStyles,
this.strutStyle, this.strutStyle,
this.textAlign, this.textAlign,
this.textDirection, this.textDirection,
@ -126,15 +127,17 @@ class LinkifyText extends StatelessWidget {
/// {@macro flutter.painting.textPainter.textWidthBasis} /// {@macro flutter.painting.textPainter.textWidthBasis}
final TextWidthBasis? textWidthBasis; final TextWidthBasis? textWidthBasis;
final Map<LinkType, TextStyle>? customLinkStyles;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Text.rich( return Text.rich(
_linkify( _linkify(
text: text, text: text,
linkStyle: linkStyle, linkStyle: linkStyle,
onTap: onTap, onTap: onTap,
linkTypes: linkTypes, linkTypes: linkTypes,
), customLinkStyles: customLinkStyles),
key: key, key: key,
style: textStyle, style: textStyle,
strutStyle: strutStyle, strutStyle: strutStyle,
@ -155,6 +158,7 @@ TextSpan _linkify({
String text = '', String text = '',
TextStyle? linkStyle, TextStyle? linkStyle,
List<LinkType>? linkTypes, List<LinkType>? linkTypes,
Map<LinkType, TextStyle>? customLinkStyles,
Function(Link)? onTap, Function(Link)? onTap,
}) { }) {
final _regExp = constructRegExpFromLinkType(linkTypes ?? [LinkType.url]); final _regExp = constructRegExpFromLinkType(linkTypes ?? [LinkType.url]);
@ -177,7 +181,7 @@ TextSpan _linkify({
spans.add( spans.add(
TextSpan( TextSpan(
text: link.value, text: link.value,
style: linkStyle, style: customLinkStyles?[link.type] ?? linkStyle,
recognizer: TapGestureRecognizer() recognizer: TapGestureRecognizer()
..onTap = () { ..onTap = () {
if (onTap != null) onTap(link); 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'; import 'package:linkfy_text/src/utils/regex.dart';
class Link { class Link {
final String? _value; late final String? _value;
final LinkType? _type; late final LinkType? _type;
String? get value => _value; String? get value => _value;
LinkType? get type => _type; LinkType? get type => _type;
/// construct link from matched regExp /// construct link from matched regExp
Link.fromMatch(RegExpMatch match) Link.fromMatch(RegExpMatch match) {
: _type = getMatchedType(match), String _match = match.input.substring(match.start, match.end);
_value = 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()); return RegExp(buffer.toString());
} }
LinkType getMatchedType(RegExpMatch match) { LinkType getMatchedType(String match) {
late LinkType type; late LinkType type;
if (RegExp(urlRegExp).hasMatch(match.input)) { if (RegExp(emailRegExp).hasMatch(match)) {
type = LinkType.url;
} else if (RegExp(hashtagRegExp).hasMatch(match.input)) {
type = LinkType.hashTag;
} else if (RegExp(emailRegExp).hasMatch(match.input)) {
type = LinkType.email; 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; type = LinkType.userTag;
} }
return type; return type;