搜尋套件、儲存庫與 APP 分類

flutter_decorated_text 封面
套件詳情v2.2.0

flutter_decorated_text

一個根據規則來樣式化和互動文字部分的組件。匹配單字、短語或模式,並套用自訂樣式或動作。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
294
喜歡數
60
Pub 分數
155

安裝套件

flutter pub add flutter_decorated_text

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 套件,可使用定義的規則對文字的特定部分進行樣式設定和互動。它允許匹配單字、詞語或模式,並套用自訂樣式或動作(例如 onTap 事件)。非常適合建立具有動態格式化的豐富文字顯示,例如強調關鍵字、嵌入連結或為特定文字區段加入動畫效果。可在所有主要平台(包括行動裝置、網頁和桌面)上運作。

適用情境

  • 在文字中強調關鍵字
  • 建立具有點擊動作的互動式文字
  • 在段落中嵌入樣式連結
  • 向特定文本段添加動畫
  • 使用動態規則顯示格式化內容
  • 聊天或文章應用中的富文本渲染

主要特色

  • 基於模式的文本匹配
  • 依匹配項目自訂樣式應用
  • 互動式回呼(例如 onTap)
  • 同時支援多個規則
  • 跨平台相容性
  • 與現有 Text 小部件的輕鬆整合

適合對象

  • 開發豐富文字介面的開發者
  • 需要關鍵字高亮的應用程式
  • 聊天或訊息應用
  • 內容豐富的教育或新聞應用
  • 具有動態文字格式化的UI需要
  • 需要互動式文字元素的專案

使用建議

  • 效能可能會影響大段文字
  • 複雜的正則表達式模式需要仔細測試
  • 有限的內建表情符號支援,僅限基本樣式
  • 需要理解文字模式匹配
  • 可能需要額外的邏輯來處理巢狀或重疊的匹配
  • 不適用於功能齊全的富文本編輯器

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Flutter DecoratedText

The DecoratedText widget allows you to style and interact with different parts of a text string based on predefined rules. It's versatile, easy-to-use, and can handle different scenarios such as matching specific words, phrases, or patterns, allowing you to style the matched text differently.

Example screenshot

Features

  • Selectable Text: Enable the selectable property (selectable: true) to allow text selection in the DecoratedText widget.
  • Predefined Rules: These are used to match different elements within the text. These rules include:
    • Matching words.
    • Matching text starting with a particular prefix.
    • Matching text between specific tags.
    • Matching links (URLs).
  • Interactive: Each rule can include an onTap callback, which returns the matched text, allowing you to implement interactive behavior such as opening a link when a URL is tapped.
  • Performance Optimized: RegExp patterns are cached internally, decoration results are memoized in text controllers, and tag deduplication uses an efficient O(n log n) algorithm.

Usage Examples

Match Text Between Tags
DecoratedText(
    text: "Like between brackets {this is an example} or html tags <p>this is a paragraph</p>",
    rules: [
        DecoratorRule.between(
            start: "{",
            end: "}",
            style: TextStyle(
                color: Colors.blue,
            ),
        ),
        DecoratorRule.between(
            start: "<p>",
            end: "</p>",
            style: TextStyle(
                color: Colors.green,
            ),
        ),
    ],
);
Match Text Starting With a Prefix
DecoratedText(
    text: "Like twitter accounts @gabdsg or hashtags #decoratedtext",
    rules: [
        DecoratorRule.startsWith(
            text: "@",
            onTap: (match) {
                print(match);
            },
            style: TextStyle(
                color: Colors.blue,
            ),
        ),
        DecoratorRule.startsWith(
            text: "#",
            onTap: (match) {
                print(match);
            },
            style: TextStyle(
                color: Colors.green,
            ),
        ),
    ],
);
Match Specific Words
DecoratedText(
    text: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.",
    rules: [
        DecoratorRule.words(
            words: ["lorem ipsum", "industry", "book", "make"],
            onTap: (match) {
                print(match);
            },
            style: TextStyle(
                background: Paint()..color = Colors.yellow,
            ),
        ),
    ],
);
Match URLs
DecoratedText(
    text: "You can match links with https https://pub.dev/ and links without it like google.com",
    rules: [
        DecoratorRule.url(
            onTap: (url) {
                print(url);
            },
            style: TextStyle(
                color: Colors.blue,
                decoration: TextDecoration.underline,
            ),
        ),
    ],
);
Custom Rule using Regular Expressions
DecoratedText(
    text: "Match links and add the favicon: https://pub.dev/, https://google.com, stackoverflow.com and talkingpts.org",
    rules: [
        DecoratorRule(
            regExp: RegExp(
                r'((https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*))',
                caseSensitive: false,
                dotAll: true,
                multiLine: true,
            ),
            onTap: (url) {
                print(url);
            },
            style: TextStyle(
                color: Colors.blue,
            ),
            leadingBuilder: (match) => Container(
                width: 16 * 0.8,
                height: 16 * 0.8,
                margin: const EdgeInsets.only(right: 2, left: 2),
                child: CachedNetworkImage(
                    imageUrl: "${sanitizeUrl(match)}/favicon.ico",
                ),
            ),
        ),
    ],
);
Match emojis
DecoratedText(
    text: "I love Flutter! 😍",
    rules: [
        DecoratorRule(
            regExp: RegExp(r'(\p{Emoji_Presentation})', unicode: true),
            style: const TextStyle(
                fontSize: 30.0,
            ),
            onTap: (emoji) {
                debugPrint('You tapped on the emoji: $emoji');
            },
        ),
    ],
);
Performance Tips

For best performance when using custom RegExp patterns, cache them outside the build() method:

// Cache at the class or file level — avoid creating inside build()
final _myRegExp = RegExp(r'my-pattern');

@override
Widget build(BuildContext context) {
    return DecoratedText(
        text: "...",
        rules: [
            DecoratorRule(regExp: _myRegExp, style: TextStyle(...)),
        ],
    );
}

Built-in rules like DecoratorRule.words(), DecoratorRule.url(), etc. handle caching automatically.

Remember, with the DecoratedText widget, you can style your text based on predefined rules, add interactivity, and handle complex matching scenarios with ease. Happy coding!

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。