搜索插件、代码仓与 APP 分类

flutter_linkify 封面
插件详情v6.0.0

flutter_linkify

将文本中的 URL 和电子邮件转换为 Flutter 中的可点击内联链接。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
28.4万
喜欢数
775
Pub 分数
150

安装插件

flutter pub add flutter_linkify

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

flutter_linkify 是一个 Flutter 包,可自动检测纯文本中的 URL 和电子邮件地址,并将其转换为可点击的链接。它通过支持内联超链接来增强文本渲染,而无需手动格式化。该包支持 Android、iOS、Web、macOS、Windows 和 Linux 等多个平台,非常适合跨平台应用。它可与 Flutter 的 Text 小部件无缝集成,并提供可自定义的链接样式和行为。

适合场景

  • 将包含网址的原始文本转换为可交互链接
  • 显示电子邮件为 mailto: 链接
  • 增强聊天或评论区的用户体验
  • 使用自动链接创建富文本内容
  • 使用可点击引用构建文档或帮助屏幕

主要特点

  • 自动检测 URL 和电子邮件
  • 对多平台的支持
  • 可自定义的链接样式
  • 与 Flutter 的 Text 小部件集成
  • 可配置的链接行为和回调
  • 处理 http 和 https 协议

适合对象

  • 需要富文本和可点击链接的应用
  • 聊天或消息界面
  • 文档或知识库应用
  • 内容密集型应用,包含用户生成的文本
  • 寻求轻松超链接集成的开发者

使用建议

  • 可能需要额外的样式设置以保持外观一致
  • 大型文本块可能带来的性能影响
  • 仅限 URL 和电子邮件模式(不支持自定义正则表达式)
  • 需要仔细处理链接点击事件
  • 不适用于复杂的富文本格式化,仅限链接

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

flutter_linkify pub package

Turns text URLs and emails into clickable inline links in text for Flutter.

Required Dart >=2.17 (has null-safety support).

Pub - API Docs - GitHub

Install

Install by adding this package to your pubspec.yaml:

dependencies:
  flutter_linkify: ^6.0.0

It is highly recommend that you also add a dependency on url_launcher to open links in the browser/OS.

Usage

Basic:

import 'package:flutter_linkify/flutter_linkify.dart';

Linkify(
  onOpen: (link) => print("Clicked ${link.url}!"),
  text: "Made by https://cretezy.com",
);
Styling

Add a style to non-links (yellow) or links (red), and open in browser using url_launcher:

import 'package:flutter_linkify/flutter_linkify.dart';
import 'package:url_launcher/url_launcher.dart';

Linkify(
  onOpen: (link) async {
    if (!await launchUrl(Uri.parse(link.url))) {
      throw Exception('Could not launch ${link.url}');
    }
  },
  text: "Made by https://cretezy.com",
  style: TextStyle(color: Colors.yellow),
  linkStyle: TextStyle(color: Colors.red),
);

All RichText options are available for use.

Linkify Options

By default, humanization (removal of http:// or https:// from the start of the URL) is enabled. You may disable it by passing options:

Linkify(
  text: "Made by https://cretezy.com",
  options: LinkifyOptions(humanize: false),
);
Selectable Text

Use the SelectableLinkify widget to create selectable text. All SelectableText options are available for use.

SelectableLinkify(
  text: "Made by https://cretezy.com\n\nMail: example@gmail.com",
);
Advance

In the onOpen callback, a LinkableElement is passed in. You can check if it is a UrlElement or EmailElement using is for custom handling.

You can enable parsing of only some link types using the linkifiers option. URL and email are enabled by default.

Full example can be found at example/lib/main.dart.

Example Screenshot

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目