migrate pkg to null safety

This commit is contained in:
iamstanlee 2021-06-03 13:52:28 -07:00
parent 3d43917444
commit 870892fdcf
6 changed files with 101 additions and 5 deletions

View File

@ -1,3 +1,44 @@
import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
enum LinkOptions { url, email, hashTag } enum LinkOptions { url, email, hashTag }
TextSpan formatText(
{required String text, TextStyle? textStyle, TextStyle? linkStyle}) {
RegExp re = RegExp(
r"(((ht|f)tp(s?)://)|www.)[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*/?([a-zA-Z0-9\-.\?,'/\\\+&%\$#_=]*)?");
// return the full text if there's no match
if (!re.hasMatch(text)) {
return TextSpan(text: text, style: textStyle);
}
List<String> texts = text.split(re);
List<TextSpan> spans = [];
List<RegExpMatch> links = re.allMatches(text).toList();
for (String text in texts) {
spans.add(TextSpan(text: text, style: textStyle));
if (links.length > 0) {
RegExpMatch match = links.removeAt(0);
String link = match.input.substring(match.start, match.end);
// add the link
spans.add(
TextSpan(
text: link,
style: linkStyle,
recognizer: TapGestureRecognizer()
..onTap = () {
String l = link.startsWith('www') ? 'http://$link' : link;
if (l.endsWith('.') || l.endsWith(',') || l.endsWith(';')) {
l = l.substring(0, l.length - 1);
}
},
),
);
}
}
return TextSpan(children: spans);
}

View File

@ -0,0 +1,7 @@
RegExp urlRegex = RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
RegExp hashtagRegex =
RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+');
RegExp emailRegex = RegExp(
r"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$");

View File

@ -0,0 +1 @@
nullify(dynamic value) {}

View File

@ -5,7 +5,7 @@ author:
homepage: homepage:
environment: environment:
sdk: ">=2.7.0 <3.0.0" sdk: '>=2.12.0 <3.0.0'
flutter: ">=1.17.0" flutter: ">=1.17.0"
dependencies: dependencies:
@ -15,7 +15,7 @@ dependencies:
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter
test: test: ^1.16.5
# For information on the generic Dart part of this file, see the # For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec # following page: https://dart.dev/tools/pub/pubspec

View File

@ -1,5 +1,3 @@
import 'package:flutter_test/flutter_test.dart'; import 'package:flutter_test/flutter_test.dart';
void main() { void main() {}
test('adds one to input values', () {});
}

49
test/regex_test.dart Normal file
View File

@ -0,0 +1,49 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:linkify_text/src/utils/regex.dart';
// final text =
// "My website url: https://blasanka.github.io/Google search using: www.google.com, social media is facebook.com, http://example.com/method?param=flutterstackoverflow.com is my greatest website. DartPad share: https://github.com/dart-lang/dart-pad/wiki/Sharing-Guide, see this example and edit it here";
void main() {
group('Regular Expression', () {
test("Should match all emails", () {
List<String> _emails = ["hello@world.com", "foo.bar@js.com"];
_emails.forEach((e) {
bool hasMatch = emailRegex.hasMatch(e);
expect(hasMatch, true);
});
});
test("Should match all urls", () {
List<String> _urls = [
"http://domain.com",
"http://domain.com/",
"https://domain.com",
"https://domain.com/",
"https://www.domain.com",
"www.domain.com",
"https://domain.com/Google?",
"https://domain.com/Google/search",
"https://domain.com/Google?param=",
"https://domain.com/Google?param=helloworld",
"https://sub.domain.com/Google?param=helloworld#hash",
];
_urls.forEach((u) {
bool hasMatch = emailRegex.hasMatch(u);
expect(hasMatch, true);
});
});
test("Should match all hashtags", () {
List<String> _hashtags = [
"#1",
"#trending",
"#_trending",
"#TRENDING",
"#trending_topic",
];
_hashtags.forEach((h) {
bool hasMatch = emailRegex.hasMatch(h);
expect(hasMatch, true);
});
});
});
}