getTextSpans method

List<InlineSpan> getTextSpans()

Implementation

List<InlineSpan> getTextSpans() {
  List<InlineSpan> widgets = <InlineSpan>[];
  RegExp reg = RegExp(
      r"([#])\w+| [@]\w+|(https?|ftp|file|#)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]*");
  Iterable<Match> matches = reg.allMatches(text);
  List<_ResultMatch> resultMatches = <_ResultMatch>[];
  int start = 0;
  for (Match match in matches) {
    if (match.group(0)!.isNotEmpty) {
      if (start != match.start) {
        _ResultMatch result1 = _ResultMatch();
        result1.isUrl = false;
        result1.text = text.substring(start, match.start);
        resultMatches.add(result1);
      }

      _ResultMatch result2 = _ResultMatch();
      result2.isUrl = true;
      result2.text = match.group(0)!;
      resultMatches.add(result2);
      start = match.end;
    }
  }
  if (start < text.length) {
    _ResultMatch result1 = _ResultMatch();
    result1.isUrl = false;
    result1.text = text.substring(start);
    resultMatches.add(result1);
  }
  for (var result in resultMatches) {
    if (result.isUrl) {
      widgets.add(_LinkTextSpan(
          onHashTagPressed: onHashTagPressed!,
          text: result.text,
          style: urlStyle != null ? urlStyle! : const TextStyle(color: Colors.blue)));
    } else {
      widgets.add(TextSpan(
          text: result.text,
          style: style ?? const TextStyle(color: Colors.black)));
    }
  }
  return widgets;
}