convertToRichText static method

Widget convertToRichText(
  1. String inputText, {
  2. TextStyle? customBaseTextStyle,
  3. dynamic onEmail(
    1. String
    )?,
})

Implementation

static Widget convertToRichText(String inputText,
    {TextStyle? customBaseTextStyle, Function(String)? onEmail}) {
  final baseTextStyle = customBaseTextStyle ?? _baseStyle;
  List<String> paragraphs = inputText.split('\n');
  List<TextSpan> textSpans = [];
  for (var paragraph in paragraphs) {
    List<String> words = paragraph.split(' ');
    for (var word in words) {
      String lastChar = '';
      _separate(word, lastChar, (p0, p1) {
        word = p0;
        lastChar = p1;
        if (EmailValidator(errorText: 'not email').isValid(word)) {
          textSpans.add(
            TextSpan(
              text: word,
              style: baseTextStyle.copyWith(color: Colors.blue),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  Pasteboard.writeText(word);
                  onEmail?.call(word);
                },
            ),
          );
          textSpans.add(
            TextSpan(
              text: lastChar.isNotEmpty ? '$lastChar ' : ' ',
              style: baseTextStyle,
            ),
          );
        } else if (word.startsWith('http://') ||
            word.startsWith('https://')) {
          textSpans.add(
            TextSpan(
              text: word,
              style: baseTextStyle.copyWith(
                color: Colors.blue,
                decoration: TextDecoration.underline,
              ),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  tryToLaunchUrl(word);
                },
            ),
          );
          textSpans.add(
            TextSpan(
              text: lastChar.isNotEmpty ? '$lastChar ' : ' ',
              style: baseTextStyle,
            ),
          );
        } else if (word.startsWith('0') &&
            NumberOnlyValidator(errorText: '').isValid(word)) {
          textSpans.add(
            TextSpan(
              text: word,
              style: baseTextStyle.copyWith(color: Colors.blue),
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  tryToLaunchUrl(word);
                },
            ),
          );
          textSpans.add(
            TextSpan(
              text: lastChar.isNotEmpty ? '$lastChar ' : ' ',
              style: baseTextStyle,
            ),
          );
        } else {
          textSpans
              .add(TextSpan(text: '$word$lastChar ', style: baseTextStyle));
        }
      });
    }
    // add the break line character at the end of the paragraph
    textSpans.add(const TextSpan(text: '\n'));
  }
  return RichText(text: TextSpan(children: textSpans));
}