Skip to content

Commit

Permalink
v1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
AnupKumarPanwar committed Jun 3, 2020
1 parent ab45720 commit c6deffd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 76 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@
## [1.0.3] - 3 June 2020.

* Fixed bug when no link is present.

## [1.0.4] - 3 June 2020.

* Convert to stateless widget.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Note: You don't need to specify the URL scheme (mailto, tel etc). The widget wil
To install the package, add the following dependency to your `pubspec.yaml`
```
dependencies:
linkable: ^3.1.3
linkable: ^1.0.4
url_launcher: ^5.4.10
```
## Usage
Expand Down
143 changes: 69 additions & 74 deletions lib/linkable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:linkable/parser.dart';
import 'package:linkable/telParser.dart';
import 'package:url_launcher/url_launcher.dart';

class Linkable extends StatefulWidget {
class Linkable extends StatelessWidget {
final String text;

final textColor;
Expand Down Expand Up @@ -39,6 +39,9 @@ class Linkable extends StatefulWidget {

final textHeightBehavior;

List<Parser> _parsers = List<Parser>();
List<Link> _links = List<Link>();

Linkable({
Key key,
@required this.text,
Expand All @@ -57,118 +60,74 @@ class Linkable extends StatefulWidget {
this.textHeightBehavior,
}) : super(key: key);

@override
_LinkableState createState() => _LinkableState();
}

class _LinkableState extends State<Linkable> {
List<Parser> _parsers = List<Parser>();
List<Link> _links = List<Link>();

@override
void initState() {
super.initState();
_addParsers();
_parseLinks();
_filterLinks();
}

@override
Widget build(BuildContext context) {
init();
return RichText(
textAlign: widget.textAlign,
textDirection: widget.textDirection,
softWrap: widget.softWrap,
overflow: widget.overflow,
textScaleFactor: widget.textScaleFactor,
maxLines: widget.maxLines,
locale: widget.locale,
strutStyle: widget.strutStyle,
textWidthBasis: widget.textWidthBasis,
textHeightBehavior: widget.textHeightBehavior,
textAlign: textAlign,
textDirection: textDirection,
softWrap: softWrap,
overflow: overflow,
textScaleFactor: textScaleFactor,
maxLines: maxLines,
locale: locale,
strutStyle: strutStyle,
textWidthBasis: textWidthBasis,
textHeightBehavior: textHeightBehavior,
text: TextSpan(
text: '',
style: widget.style,
style: style,
children: _getTextSpans(),
),
);
}

_addParsers() {
_parsers.add(EmailParser(widget.text));
_parsers.add(HttpParser(widget.text));
_parsers.add(TelParser(widget.text));
}

_parseLinks() {
for (Parser parser in _parsers) {
_links.addAll(parser.parse().toList());
}
}

_filterLinks() {
_links.sort(
(Link a, Link b) => a.regExpMatch.start.compareTo(b.regExpMatch.start));

List<Link> _filteredLinks = List<Link>();
if (_links.length > 0) {
_filteredLinks.add(_links[0]);
}

for (int i = 0; i < _links.length - 1; i++) {
if (_links[i + 1].regExpMatch.start > _links[i].regExpMatch.end) {
_filteredLinks.add(_links[i + 1]);
}
}
_links = _filteredLinks;
}

_getTextSpans() {
List<TextSpan> _textSpans = List<TextSpan>();
int i = 0;
int pos = 0;
while (i < widget.text.length) {
_textSpans.add(_text(widget.text.substring(
while (i < text.length) {
_textSpans.add(_text(text.substring(
i,
pos < _links.length && i < _links[pos].regExpMatch.start
pos < _links.length && i <= _links[pos].regExpMatch.start
? _links[pos].regExpMatch.start
: widget.text.length)));
if (pos < _links.length && i < _links[pos].regExpMatch.start) {
: text.length)));
if (pos < _links.length && i <= _links[pos].regExpMatch.start) {
_textSpans.add(_link(
widget.text.substring(
text.substring(
_links[pos].regExpMatch.start, _links[pos].regExpMatch.end),
_links[pos].type));
i = _links[pos].regExpMatch.end;
pos++;
} else {
i = widget.text.length;
i = text.length;
}
}
return _textSpans;
}

_launch(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}

_text(String text) {
return TextSpan(text: text, style: TextStyle(color: widget.textColor));
return TextSpan(text: text, style: TextStyle(color: textColor));
}

_link(String text, String type) {
return TextSpan(
text: text,
style: TextStyle(color: widget.linkColor),
style: TextStyle(color: linkColor),
recognizer: TapGestureRecognizer()
..onTap = () {
_launch(_getUrl(text, type));
});
}

_launch(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}

_getUrl(String text, String type) {
switch (type) {
case http:
Expand All @@ -181,4 +140,40 @@ class _LinkableState extends State<Linkable> {
return text;
}
}

init() {
_addParsers();
_parseLinks();
_filterLinks();
}

_addParsers() {
_parsers.add(EmailParser(text));
_parsers.add(HttpParser(text));
_parsers.add(TelParser(text));
}

_parseLinks() {
for (Parser parser in _parsers) {
_links.addAll(parser.parse().toList());
}
}

_filterLinks() {
_links.sort(
(Link a, Link b) =>
a.regExpMatch.start.compareTo(b.regExpMatch.start));

List<Link> _filteredLinks = List<Link>();
if (_links.length > 0) {
_filteredLinks.add(_links[0]);
}

for (int i = 0; i < _links.length - 1; i++) {
if (_links[i + 1].regExpMatch.start > _links[i].regExpMatch.end) {
_filteredLinks.add(_links[i + 1]);
}
}
_links = _filteredLinks;
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: linkable
description: A Flutter widget to add links to your text. Linkable widget is a wrapper over RichText which allows you to render links that can be clicked to redirect to the URL.
version: 1.0.3
version: 1.0.4
homepage: https://github.com/AnupKumarPanwar/Linkable

environment:
Expand Down

0 comments on commit c6deffd

Please sign in to comment.