Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for mailto: links. #8

Merged
merged 3 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_linkify/flutter_linkify.dart';
import 'dart:async';

import 'package:url_launcher/url_launcher.dart';

void main() => runApp(new LinkifyExample());
Expand All @@ -15,17 +17,25 @@ class LinkifyExample extends StatelessWidget {
),
body: Center(
child: Linkify(
onOpen: (url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
onLinkOpen: (url) {
_launchLink(url);
},
onEmailOpen: (emailAddress) {
final emailAddressLink = "mailto:$emailAddress";
_launchLink(emailAddressLink);
},
text: "Made by https://cretezy.com",
text: "Made by https://cretezy.com\n\nMail: example@gmail.com",
),
),
),
);
}

Future _launchLink(String link) async {
if (await canLaunch(link)) {
await launch(link);
} else {
throw 'Could not launch $link';
}
}
}
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
url_launcher: ^3.0.3
url_launcher: ^5.0.1
flutter_linkify:
path: ..

Expand Down
39 changes: 31 additions & 8 deletions lib/flutter_linkify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import 'package:flutter_linkify/linkify.dart';
/// Callback with URL to open
typedef LinkCallback(String url);

/// Callback with Email address
typedef EmailCallback(String emailAddress);

/// Turns URLs into links
class Linkify extends StatelessWidget {
/// Text to be linkified
Expand All @@ -17,8 +20,12 @@ class Linkify extends StatelessWidget {
final TextStyle linkStyle;

/// Callback for tapping a link
final LinkCallback onOpen;
final LinkCallback onLinkOpen;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breaking change, will require a major bump.


/// Callback for tapping an email
final EmailCallback onEmailOpen;

/// Text direction of the text
final TextDirection textDirection;

/// Removes http/https from shown URLS
Expand All @@ -29,7 +36,8 @@ class Linkify extends StatelessWidget {
this.text,
this.style,
this.linkStyle,
this.onOpen,
this.onLinkOpen,
this.onEmailOpen,
this.textDirection,
this.humanize = false,
}) : super(key: key);
Expand All @@ -51,7 +59,8 @@ class Linkify extends StatelessWidget {
decoration: TextDecoration.underline,
)
.merge(linkStyle),
onOpen: onOpen,
onLinkOpen: onLinkOpen,
onEmailOpen: onEmailOpen,
humanize: humanize,
),
);
Expand All @@ -63,12 +72,19 @@ TextSpan buildTextSpan({
String text,
TextStyle style,
TextStyle linkStyle,
LinkCallback onOpen,
LinkCallback onLinkOpen,
EmailCallback onEmailOpen,
bool humanize = false,
}) {
void _onOpen(String url) {
if (onOpen != null) {
onOpen(url);
void _onLinkOpen(String url) {
if (onLinkOpen != null) {
onLinkOpen(url);
}
}

void _onEmailOpen(String emailAddress) {
if (onEmailOpen != null) {
onEmailOpen(emailAddress); // TODO: discussable; add "mailto:" here for immediate use with url_launcher or except developers to do it themselves
}
}

Expand All @@ -90,7 +106,14 @@ TextSpan buildTextSpan({
text: element.text,
style: linkStyle,
recognizer: TapGestureRecognizer()
..onTap = () => _onOpen(element.url),
..onTap = () => _onLinkOpen(element.url),
);
} else if (element is EmailElement) {
return TextSpan(
text: element.text,
style: linkStyle,
recognizer: TapGestureRecognizer()
..onTap = () => _onEmailOpen(element.emailAddress),
);
}
},
Expand Down
58 changes: 45 additions & 13 deletions lib/linkify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ class LinkElement extends LinkifyElement {
}
}

/// Represents an element containing an email address
class EmailElement extends LinkifyElement {
final String emailAddress;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a case where the email address != displayed address?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not now I think.. But it will be when support for ?subject=...&body=... is added.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's still gonna be the same though, since subject, body, etc is simply gonna be properties (not shown)

final String text;

EmailElement(this.emailAddress, [String text]) : this.text = text ?? emailAddress;

@override
String toString() {
return "EmailElement: $emailAddress ($text)";
}
}

/// Represents an element containing text
class TextElement extends LinkifyElement {
final String text;
Expand All @@ -25,11 +38,16 @@ class TextElement extends LinkifyElement {
}
}

final _linkifyRegex = RegExp(
final _linkifyUrlRegex = RegExp(
r"(\n*?.*?\s*?)((?:https?):\/\/[^\s/$.?#].[^\s]*)",
caseSensitive: false,
);

final _linkifyEmailRegex = RegExp(
r"(\n*?.*?\s*?)((mailto:)?[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})",
caseSensitive: false
);

/// Turns [text] into a list of [LinkifyElement]
///
/// Use [humanize] to remove http/https from the start of the URL shown.
Expand All @@ -40,29 +58,43 @@ List<LinkifyElement> linkify(String text, {bool humanize = false}) {
return list;
}

final match = _linkifyRegex.firstMatch(text);
if (match == null) {
final urlMatch = _linkifyUrlRegex.firstMatch(text);
final emailMatch = _linkifyEmailRegex.firstMatch(text);
if (urlMatch == null && emailMatch == null) {
list.add(TextElement(text));
} else {
text = text.replaceFirst(_linkifyRegex, "");
} else if (urlMatch != null){
text = text.replaceFirst(_linkifyUrlRegex, "");

if (match.group(1).isNotEmpty) {
list.add(TextElement(match.group(1)));
if (urlMatch.group(1).isNotEmpty) {
list.add(TextElement(urlMatch.group(1)));
}

if (match.group(2).isNotEmpty) {
if (urlMatch.group(2).isNotEmpty) {
if (humanize ?? false) {
print("humanizing ${match.group(2)}");
list.add(LinkElement(
match.group(2),
match.group(2).replaceFirst(RegExp(r"https?://"), ""),
urlMatch.group(2),
urlMatch.group(2).replaceFirst(RegExp(r"https?://"), ""),
));
} else {
print("not humanizing ${match.group(2)}");
list.add(LinkElement(match.group(2)));
list.add(LinkElement(urlMatch.group(2)));
}
}

list.addAll(linkify(text, humanize: humanize));
} else if (emailMatch != null) {
text = text.replaceFirst(_linkifyEmailRegex, "");

if (emailMatch.group(1).isNotEmpty) {
list.add(TextElement(emailMatch.group(1)));
}

if (emailMatch.group(2).isNotEmpty) {
// Always humanize emails
list.add(EmailElement(
emailMatch.group(2).replaceFirst(RegExp(r"mailto:"), "")
));
}

list.addAll(linkify(text, humanize: humanize));
}

Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_linkify
description: Turns text URLs into clickable inline links in text for Flutter.
version: 1.1.1
description: Turns text URLs and Mailto links into clickable inline links in text for Flutter.
version: 1.2.0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version: 1.2.0
version: 2.0.0

To match the breaking change above.

We could support both onOpen and onOpenLink (and deprecate onOpen). To to you to make the decision!

author: Charles Crete <charles@cretezy.com>
homepage: https://github.com/Cretezy/flutter_linkify

Expand Down