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

flutter_parsed_text 封面
插件详情v2.2.1

flutter_parsed_text

一个用于解析文本并将其转换为多个 Flutter Text 小部件的 Flutter 包。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
25.2万
喜欢数
270
Pub 分数
150

安装插件

flutter pub add flutter_parsed_text

插件介绍

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

一个 Flutter 包,可实现将文本解析为自动转换特定模式(如 URL、电子邮件或标签)为可交互的 Text 小部件。它简化了带有可点击元素的格式化文本渲染过程,提升了显示动态内容的应用程序的用户体验。该包支持多个平台,并可无缝集成到现有的 Flutter 项目中。它非常适合聊天界面、社交媒体动态流或任何需要丰富文本解析的 UI。

适合场景

  • 解析聊天消息中的URL
  • 突出显示标签和提及
  • 创建可点击的电子邮件链接
  • 在社交动态中渲染富文本
  • 显示格式化的用户生成内容

主要特点

  • 自动检测网址、电子邮件和标签
  • 可自定义的已解析文本样式
  • 支持多个平台目标
  • 与现有 Text 小部件的轻松集成
  • 可扩展的模式匹配系统

适合对象

  • 聊天和消息应用
  • 社交媒体应用
  • 内容丰富的用户界面,带有动态文本
  • 需要交互式文本元素的应用程序
  • 寻求轻量级文本解析的开发者

使用建议

  • 可能需要自定义正则表达式以支持高级模式
  • 长文本带来的性能影响
  • 对复杂格式的内置支持有限
  • 需要仔细处理嵌套的文本结构
  • 不适用于实时流式文本

原始 README

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

🔗 Flutter Parsed text

A Flutter package to parse text and extract parts using predefined types like url, phone and email and also supports Regex.

Usage 💻

To use this package, add flutter_parsed_text as a dependency in your pubspec.yaml file.

import 'package:flutter_parsed_text/flutter_parsed_text.dart';

Working ⚙️

ParsedText can receive this paramters & all the RichText parameters:

text: Text that will be parsed and rendered.

style: It takes a TextStyle object as it's property to style all the non links text objects.

parse: Array of MatchText object - used for defining structure for pattern matching .

MatchText(
  type: ParsedType.EMAIL, // predefined type can be any of this ParsedTypes
  style: TextStyle(
    color: Colors.red,
    fontSize: 24,
  ), // custom style to be applied to this matched text
  onTap: (url) {
    // do something here with passed url
  }, // callback funtion when the text is tapped on
),

You can also define a custom pattern like this:

MatchText(
  pattern: r"\B#+([\w]+)\b", // a custom pattern to match
  style: TextStyle(
    color: Colors.pink,
    fontSize: 24,
  ), // custom style to be applied to this matched text
  onTap: (url) async {
  // do something here with passed url
  }, // callback funtion when the text is tapped on
)

You can also set RegexOption for the custom regex pattern like so:

MatchText(
  pattern: r"\B#+([\w]+)\b", // a custom pattern to match
  regexOptions: RegexOptions(
    multiLine : false,
    caseSensitive : false,
    unicode : false,
    dotAll : false
  ),
  style: TextStyle(
    color: Colors.pink,
    fontSize: 24,
  ), // custom style to be applied to this matched text
  onTap: (url) async {
  // do something here with passed url
  }, // callback funtion when the text is tapped on
)

A boolean that show a diffrent text and passes a diffrent text to the callback

eg: Your str is "Mention [@michel:5455345]" where 5455345 is ID of this user which will be passed as parameter to the callback funtion and @michel the value to display on interface. Your pattern for ID & username extraction : /\[(@[^:]+):([^\]]+)\]/i

Displayed text will be : Mention ^^@michel^^

MatchText(
  pattern: r"\[(@[^:]+):([^\]]+)\]",
  style: TextStyle(
    color: Colors.green,
    fontSize: 24,
  ),
  // you must return a map with two keys
  // [display] - the text you want to show to the user
  // [value] - the value underneath it
  renderText: ({String str, String pattern}) {
    Map<String, String> map = Map<String, String>();
    RegExp customRegExp = RegExp(pattern);
    Match match = customRegExp.firstMatch(str);
    map['display'] = match.group(1);
    map['value'] = match.group(2);
    return map;
  },
  onTap: (url) {
    // do something here with passed url
  },
),

Example ✍🏻

Find the complete example wiring in the Flutter_Parsed_Text example application.

A small example of the ParsedText widget.

ParsedText(
  text:
    "[@michael:51515151] Hello this is an example of the ParsedText, links like http://www.google.com or http://www.facebook.com are clickable and phone number 444-555-6666 can call too. But you can also do more with this package, for example Bob will change style and David too. foo@gmail.com And the magic number is 42! #react #react-native",
  parse: <MatchText>[
    MatchText(
      type: ParsedType.EMAIL,
      style: TextStyle(
        color: Colors.red,
        fontSize: 24,
      ),
      onTap: (url) {
        launch("mailto:" + url);
      },
    ),
  ],
)

Found this project useful? ❤️

If you found this project useful, then please consider giving it a ⭐️ on Github and sharing it with your friends via social media.

API details 👨🏻‍💻

See the flutter_parsed_text.dart for more API details

License ⚖️

Issues and feedback 💭

If you have any suggestion for including a feature or if something doesn't work, feel free to open a Github issue for us to have a discussion on it.

所属分类

源码仓库

使用此插件的项目