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:
commit
8da1999a72
|
@ -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,
|
||||||
|
|
|
@ -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,6 +127,8 @@ 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(
|
||||||
|
@ -134,7 +137,7 @@ class LinkifyText extends StatelessWidget {
|
||||||
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);
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in New Issue