findURLs method
Implementation
List<TextSpan> findURLs(
String text,
BuildContext context,
TextStyle regularTextStyle,
) {
final List<TextSpan> children = [];
final RegExp exp = RegExp(
r"((https?:www\.)|(https?:\/\/)|(www\.))[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}(\/[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)?",
);
final List<Match> matches = exp.allMatches(text).toList();
int cursorPosition = 0;
for (int i = 0; i < matches.length; i += 1) {
final Match match = matches[i];
final Uri url = Uri.parse(text.substring(match.start, match.end));
children.add(
TextSpan(
text: text.substring(cursorPosition, match.start),
style: regularTextStyle,
),
);
children.add(
TextSpan(
text: text.substring(match.start, match.end),
style: TextStyle(color: Theme.of(context).colorScheme.secondary),
recognizer: TapGestureRecognizer()
..onTap =
() => launchUrl(url, mode: LaunchMode.externalApplication),
),
);
cursorPosition = match.end;
}
children.add(
TextSpan(
text: text.substring(cursorPosition, text.length),
style: regularTextStyle,
),
);
return children;
}