超鏈接識(shí)別,http開(kāi)頭和不是http開(kāi)頭常用的一些

import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class LinkText extends StatelessWidget {
  final String text;
  final TextStyle? style;
  final TextStyle? linkStyle;

  const LinkText({
    super.key,
    required this.text,
    this.style,
    this.linkStyle,
  });

  @override
  Widget build(BuildContext context) {
    // ??【超嚴(yán)格正則】能識(shí)別 http/https/www/普通域名,且不會(huì)誤判普通文字
    final regExp = RegExp(
      r'''(https?:\/\/[^\s]+)|''' // http/https 鏈接
      r'''(www\.[a-z0-9\-]+\.[^\s]+)|''' // www 開(kāi)頭鏈接
      r'''([a-z0-9\-]+\.(com|cn|net|org|io|app|dev|top|xyz|cc|co|gov|edu)(\/[^\s]*)?)''', // 純域名(不帶http/www)
      caseSensitive: false,
    );

    final List<TextSpan> children = [];
    final matches = regExp.allMatches(text);
    int start = 0;

    for (final match in matches) {
      // 普通文本
      if (match.start > start) {
        children.add(TextSpan(
          text: text.substring(start, match.start),
          style: style,
        ));
      }

      // 鏈接文本
      final link = text.substring(match.start, match.end);
      children.add(TextSpan(
        text: link,
        style: linkStyle ??
            const TextStyle(
              color: Colors.blue,
              decoration: TextDecoration.underline,
              height: 1.3,
            ),
        recognizer: TapGestureRecognizer()..onTap = () => _openUrl(link),
      ));

      start = match.end;
    }

    // 剩余文本
    if (start < text.length) {
      children.add(TextSpan(
        text: text.substring(start),
        style: style,
      ));
    }

    return RichText(
      text: TextSpan(
        style: style ??
            const TextStyle(
              color: Colors.black87,
              fontSize: 15,
            ),
        children: children,
      ),
    );
  }

  // 打開(kāi)鏈接(自動(dòng)補(bǔ)全協(xié)議)
  Future<void> _openUrl(String url) async {
    String targetUrl = url;
    if (!url.startsWith(RegExp(r'https?://'))) {
      targetUrl = 'https://$url';
    }

    final uri = Uri.parse(targetUrl);
    if (await canLaunchUrl(uri)) {
      await launchUrl(uri, mode: LaunchMode.externalApplication);
    }
  }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容