搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案