搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目

暂无相关数据。