parse method

TextSpan parse(
  1. String text
)

Implementation

TextSpan parse(String text) {
  final linkRegExp = RegExp(r'\[(.*?)\]\((.*?)\)');
  final match = linkRegExp.firstMatch(text);
  if (match == null) {
    return TextSpan(text: text, style: style);
  } else {
    final before = text.substring(0, match.start);
    final after = text.substring(match.end);
    final linkText = match.group(1);
    final link = match.group(2);
    return TextSpan(
      style: style,
      children: [
        TextSpan(text: before),
        TextSpan(
          text: linkText,
          style: style?.merge(linkStyle),
          recognizer: TapGestureRecognizer()
            ..onTap = () async {
              final uri = Uri.parse(link!);
              if (await canLaunchUrl(uri)) {
                await launchUrl(uri);
              } else {
                throw 'Could not launch $link';
              }
            },
        ),
        parse(after),
      ],
    );
  }
}