parseHttpLink method

List<InlineSpan> parseHttpLink(
  1. {String content,
  2. LinkUrlConfig linkUrlConfig = const LinkUrlConfig.defaultConfig()}
)

Implementation

static List<InlineSpan> parseHttpLink({
  String content,
  LinkUrlConfig linkUrlConfig = const LinkUrlConfig.defaultConfig(),
}) {
  assert(content != null && content.length > 0);
  List<InlineSpan> result = [];
  List<Match> matches = regExp.allMatches(content).toList();
  if (matches.length == 0) {
    //没有超链接
    result.add(TextSpan(text: content, style: linkUrlConfig.contentStyle));
  } else {
    //有超链接
    int index = 0;
    for (int i = 0; i < matches.length; i++) {
      Match m = matches[i];
      if (m.start > index) {
        result.add(TextSpan(
            text: content.substring(
                index, m.start > content.length ? content.length : m.start),
            style: linkUrlConfig.contentStyle));
      }
      String url = content.substring(
          m.start, m.end > content.length ? content.length : m.end);

      if (linkUrlConfig.enableLinkIcon) {
        result.add(
          WidgetSpan(
              child: Image.asset(
                  linkUrlConfig.linkIconPath),
              alignment: ui.PlaceholderAlignment.middle),
        );
      }
      result.add(
        TextSpan(
            text: linkUrlConfig.linkText ?? "",
            recognizer: TapGestureRecognizer()
              ..onTap = () {
                if (linkUrlConfig.onLinkTap != null) {
                  linkUrlConfig.onLinkTap.call(url);
                }
              },
            style: linkUrlConfig.linkStyle),
      );

      index = m.end;
    }

    if (content.length > index) {
      result.add(TextSpan(
          text: content.substring(index, content.length),
          style: linkUrlConfig.contentStyle));
    }
  }
  return result;
}