パッケージ、リポジトリ、アプリカテゴリを検索

flutter_decorated_text のカバー
パッケージ詳細v2.2.0

flutter_decorated_text

ルールに基づいてテキストの一部をスタイル設定およびインタラクションするためのウィジェット。単語、フレーズ、またはパターンに一致させ、カスタムスタイルまたはアクションを適用します。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
294
いいね数
60
Pub ポイント
155

パッケージをインストール

flutter pub add flutter_decorated_text

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

定義されたルールを使用してテキストの特定部分にスタイルを適用し、インタラクションを行うためのFlutterパッケージです。単語、フレーズ、パターンをマッチさせ、カスタムスタイルやタップイベントなどのアクションを適用できます。キーワードのハイライト、リンクの埋め込み、特定のテキストセグメントへのアニメーションの追加など、動的なフォーマットを持つリッチテキスト表示を作成するのに最適です。モバイル、Web、デスクトップを含むすべての主要プラットフォームで動作します。

用途

  • テキスト内のキーワードを強調する
  • タップ操作付きのインタラクティブテキストを作成する
  • 段落内にスタイル付きリンクを埋め込む
  • 特定のテキストセグメントにアニメーションを追加する
  • 動的ルールでフォーマットされたコンテンツを表示する
  • チャットや記事アプリにおけるリッチテキストのレンダリング

主な機能

  • パターンベースのテキスト一致
  • 一致ごとにカスタムスタイルを適用
  • インタラクティブなコールバック(例: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 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。