如何在Flutter中链接文本 [英] How to linkify text in Flutter
问题描述
Flutter中是否有一种简单的方法来链接"可能包含纯文本,电子邮件和Web URL的文本?例如.如果我的文字是我的电话号码是099 123 45 67,而我的电子邮件是test@test.com
,则该电话号码和电子邮件将显示为可点击的链接.
Is there an easy way in Flutter to 'linkify' a text that might contain a mix of plain text, emails and web URLs? E.g. if my text is My phone number is 099 123 45 67 and my email is test@test.com
the phone number and the email would be rendered as clickable links.
在Android中,它将是一个衬里:
In Android it would be a one liner:
textView.setAutoLinkMask(Linkify.ALL);
我已经看到在此处.该解决方案适用于静态文本,但对于动态文本,解析文本,检测所有URL,电话号码,电子邮件等,并使用 TextSpan
进行相应渲染会复杂得多.
I've seen that a similar question has been asked here. That solution would work fine for static texts, but for dynamic texts it would be a lot more complicated to parse the text, detect all URLs, phone numbers, emails etc. and use TextSpan
s to render them accordingly.
推荐答案
我为此创建了一个新程序包:flutter_linkify .它目前仅支持URLS,但是您始终可以通过功能请求在 GitHub 上提交问题.
I created a new package for this: flutter_linkify. It currently only supports URLS, but you can always submit an issue on GitHub with feature requests.
基本用法:
import 'package:flutter_linkify/flutter_linkify.dart';
Linkify(
onOpen: (url) => print("Clicked $url!"),
text: "Made by https://cretezy.com",
);
这篇关于如何在Flutter中链接文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!