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,
|
||||
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,
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue