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

easy_rich_text 封面
插件详情v2.2.0

easy_rich_text

EasyRichText 小部件提供了一种使用 RichText 的简便方法。您无需手动拆分字符串。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3.2万
喜欢数
250
Pub 分数
130

安装插件

flutter pub add easy_rich_text

插件介绍

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

EasyRichText 通过自动处理文本分段和样式,简化了 Flutter 中 RichText 的使用,无需手动拆分字符串。它允许开发者使用简单语法,在单个文本字符串中直接应用不同的样式、颜色和小部件。适用于显示混合样式的格式化文本,例如链接、提及或表情符号,可减少样板代码并提高可读性。该包支持所有主要平台,包括移动设备、网页和桌面端。

适合场景

  • 显示带有混合格式的样式文本
  • 创建带有嵌入链接的富文本
  • 在文本中突出显示关键词
  • 使用样式渲染用户生成的内容
  • 在格式化文本中添加表情符号

主要特点

  • 自动文本分段
  • 支持内联样式
  • 简洁语法,用于复杂格式化
  • 跨平台兼容性
  • 最小化样板代码

适合对象

  • 寻求简单富文本渲染的开发者
  • 包含动态或用户生成内容的应用程序
  • 聊天或消息界面
  • 带有格式化文本的文档或帮助系统

使用建议

  • 与完整 RichText 相比,样式功能有限
  • 处理非常大的文本时可能会影响性能
  • 需要仔细处理嵌套小部件
  • 不太适合高度复杂的布局
  • 依赖外部库的更新以获取新功能

原始 README

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

easy_rich_text

pub package GitHub license GitHub stars

The EasyRichText widget makes the RichText widget easy. You do not have to split the string manually.

This widget use regular expression to effectively split the string based on the patterns defined in the list of EasyRichTextPattern.

The EasyRichTextPattern is a class defines the text pattern you want to format.

targetString can be a String or a List<String>.

By default matchWordBoundaries:true is set to match the whole word. If you want to match substring in a word, set matchWordBoundaries:false

GestureRecognizer and url_launcher are integrated.

If you find this package useful, I would appreciate it if you can give me a star on Github and a like on pub.dev

Getting Started

Installing:
dependencies:
  easy_rich_text: '^2.0.0'
Examples:

Simple Example | Trademark Example | Default Style | Conditional Match | Match Option | Superscript and Subscript | Case Sensitivity | Selectable Text | Regular Expression | Url Launcher | GestureRecognizer | All RichText Properties | Special Characters | WhatsApp Like Text Formatter

Simple Example:

alt text

EasyRichText(
  "I want blue font. I want bold font. I want italic font.",
  patternList: [
    EasyRichTextPattern(
      targetString: 'blue',
      style: TextStyle(color: Colors.blue),
    ),
    EasyRichTextPattern(
      targetString: 'bold',
      style: TextStyle(fontWeight: FontWeight.bold),
    ),
    EasyRichTextPattern(
      targetString: 'italic',
      style: TextStyle(fontStyle: FontStyle.italic),
    ),
  ],
),
Trademark Example

alt text

EasyRichText(
  "ProductTM is a superscript trademark symbol. This TM is not a trademark.",
  patternList: [
    EasyRichTextPattern(
      targetString: 'TM',
      superScript: true,
      stringBeforeTarget: 'Product',
      matchWordBoundaries: false,
      style: TextStyle(color: Colors.blue),
    ),
  ],
),
Default Style:

alt text

EasyRichText(
  "This is a EasyRichText example with default grey font. I want blue font here.",
  defaultStyle: TextStyle(color: Colors.grey),
  patternList: [
    EasyRichTextPattern(
      targetString: 'blue',
      style: TextStyle(color: Colors.blue),
    ),
    EasyRichTextPattern(
      targetString: 'bold',
      style: TextStyle(fontWeight: FontWeight.bold),
    ),
  ],
),
Conditional Match

alt text

EasyRichText(
  "I want blue color here. I want no blue font here. I want blue invalid here.",
  patternList: [
    EasyRichTextPattern(
      targetString: 'blue',
      stringBeforeTarget: 'want',
      stringAfterTarget: "color",
      style: TextStyle(color: Colors.blue),
    ),
  ],
),
Match Option

alt text

matchOption can be a string or a list. default is 'all'.
string: 'all', 'first', 'last'
List:'first', 'last', and any integer index
For example, [0, 1, 'last'] will match the first, second, and last one.
EasyRichText(
  "blue 1, blue 2, blue 3, blue 4, blue 5",
  patternList: [
    EasyRichTextPattern(
      targetString: 'blue',
      //matchOption: 'all'
      matchOption: [0, 1, 'last'],
      style: TextStyle(color: Colors.blue),
    ),
  ],
),
Superscript and Subscript.

alt text

EasyRichText(
  "I want superscript font here. I want subscript here",
  patternList: [
    EasyRichTextPattern(
        targetString: 'superscript', superScript: true),
    EasyRichTextPattern(
        targetString: 'subscript', subScript: true),
  ],
),
Case Sensitivity

alt text

EasyRichText(
  "Case-Insensitive String Matching. I want both Blue and blue. This paragraph is selectable.",
  caseSensitive: false,
  selectable: true,
  patternList: [
    EasyRichTextPattern(
      targetString: 'Blue',
      style: TextStyle(color: Colors.blue),
    ),
  ],
),
Selectable Text

alt text

EasyRichText(
  "This paragraph is selectable...",
  selectable: true,
),
Regular Expression

alt text

EasyRichText(
  "Regular Expression. I want blue bluea blue1 but not blueA",
  patternList: [
    EasyRichTextPattern(
      targetString: 'bl[a-z0-9]*',
      style: TextStyle(color: Colors.blue),
    ),
  ],
),
Url Launcher

Integrated with url_launcher. Web url, email url, and telephone url are supported. Set urlType : 'web', 'email', or 'tel'. EasyRichText provides regular expression formula to match common urls.

alt text

EasyRichText(
  "Here is a website https://pub.dev/packages/easy_rich_text. Here is a email address test@example.com. Here is a telephone number +852 12345678.",
  patternList: [
    EasyRichTextPattern(
      targetString: 'https://pub.dev/packages/easy_rich_text',
      urlType: 'web',
      style: TextStyle(
        decoration: TextDecoration.underline,
      ),
    ),
    EasyRichTextPattern(
      targetString: EasyRegexPattern.emailPattern,
      urlType: 'email',
      style: TextStyle(
        decoration: TextDecoration.underline,
      ),
    ),
    EasyRichTextPattern(
      targetString: EasyRegexPattern.webPattern,
      urlType: 'web',
      style: TextStyle(
        decoration: TextDecoration.underline,
      ),
    ),
    EasyRichTextPattern(
      targetString: EasyRegexPattern.telPattern,
      urlType: 'tel',
      style: TextStyle(
        decoration: TextDecoration.underline,
      ),
    ),
  ],
),
GestureRecognizer
///GestureRecognizer, not working when superscript, subscript, or urlType is set.
///TapGestureRecognizer, MultiTapGestureRecognizer, etc.
EasyRichText(
  "Tap recognizer to print this sentence.",
  patternList: [
    EasyRichTextPattern(
      targetString: 'recognizer',
      recognizer: TapGestureRecognizer()
        ..onTap = () {
          print("Tap recognizer to print this sentence.");
        },
      style: TextStyle(
        decoration: TextDecoration.underline,
      ),
    ),
  ],
),
All RichText Properties

alt text

EasyRichText(
  "TextOverflow.ellipsis, TextAlign.justify, maxLines: 1. TextOverflow.ellipsis, TextAlign.justify, maxLines: 1.",
  textAlign: TextAlign.justify,
  maxLines: 1,
  overflow: TextOverflow.ellipsis,
),
Special Characters

alt text

//if the targetString contains the following special characters \[]()^*+?
EasyRichText(
  "Received 88+ messages. Received 99+ messages",
  patternList: [
    //set hasSpecialCharacters to true
    EasyRichTextPattern(
      targetString: '99+',
      hasSpecialCharacters: true,
      style: TextStyle(color: Colors.blue),
    ),
    //or if you are familiar with regular expressions, then use \\ to skip it
    EasyRichTextPattern(
      targetString: '88\\+',
      style: TextStyle(color: Colors.blue),
    ),
  ],
),
WhatsApp Like Text Formatter

alt text

///WhatsApp like text formatter
EasyRichText(
  "TEST *bold font*. test *boldfont*.",
  patternList: [
    ///bold font
    EasyRichTextPattern(
      targetString: '(\\*)(.*?)(\\*)',
      matchBuilder: (BuildContext context, RegExpMatch? match) {
        return TextSpan(
          text: match?[0]?.replaceAll('*', ''),
          style: const TextStyle(fontWeight: FontWeight.bold),
        );
      },
    ),
  ],
),
prefixInlineSpan & suffixInlineSpan
///add icon before/after targetString
EasyRichText(
  "Please contact us at +123456789",
  patternList: [
    EasyRichTextPattern(
      targetString: EasyRegexPattern.telPattern,
      prefixInlineSpan: WidgetSpan(
        child: Icon(Icons.local_phone),
      ),
      suffixInlineSpan: WidgetSpan(
        child: Icon(Icons.local_phone),
      ),
    )
  ],
),

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目