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

插件详情v2.2.2

contentful_rich_text

解析 Contentful 富文本 JSON 对象并返回可渲染的 Flutter widget 树的富文本渲染器

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.2万
喜欢数
29
Pub 分数
140

安装插件

flutter pub add contentful_rich_text

插件介绍

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

一个 Flutter 包,可将 Contentful 富文本 JSON 渲染为原生 Flutter widget 树。它解析来自 Contentful 的结构化富文本内容,支持格式化、链接、图片和嵌套块,使 Flutter 应用能够实现动态且丰富的用户界面。该包专为与 Contentful 的无头 CMS 无缝集成而设计,使开发者无需手动构建 widget 即可显示编辑内容。

适合场景

  • 在移动和Web应用程序中渲染CMS生成的富文本
  • 显示格式化的文章或博客帖子
  • 嵌入交互式内容,如链接和图像
  • 支持多语言内容分发
  • 从结构化数据构建动态 UI

主要特点

  • 直接解析 Contentful 富文本 JSON
  • 支持内联和块级格式化
  • 内置链接、图片和列表处理
  • 通过自定义渲染器实现可扩展的部件映射
  • 跨平台兼容性,适用于所有 Flutter 目标

适合对象

  • 使用 Contentful 作为无头 CMS 的 Flutter 开发人员
  • 需要从后端获取丰富可编辑内容的应用程序
  • 通过CMS管理内容而不硬编码UI的团队

使用建议

  • 需要正确设置 Contentful 内容模型
  • 复杂的布局可能需要自定义渲染逻辑
  • 性能取决于渲染内容的复杂性
  • 受限于 Contentful 的富文本格式规范

原始 README

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

Contentful Rich Text Renderer for Flutter

Rich Text renderer that parses Contentful Rich Text field JSON output and produces a Flutter Widget tree that can be displayed in a Flutter app.

Installation

To use this plugin, add contentful_rich_text as a dependency in your pubspec.yaml file.

Usage

Note: The json examples are samples of what Contentful's API can return. The getContentfulJson method is just a placeholder for where your code would call a method to get your data from the Contentful API.

Simple document with paragraph, no inline marks (bold/italic/underline)
Sample JSON
{
  "nodeType": "document",
  "content": [
    {
      "nodeType": "paragraph",
      "content": [
        {
          "nodeType": "text",
          "value": "Hello world!",
          "marks": []
        }
      ]
    }
  ]
}
Sample Dart implementation
import 'package:contentful_rich_text/contentful_rich_text.dart';

// getting data from Contentful, must be implemented in your project
// document will be a dynamic variable JSON map matching the above JSON
var document = getContentfulJson(); 

class Demo extends StatelessWidget {

@override
  Widget build(BuildContext context) {
    return ContentfulRichText(document).documentToWidgetTree;
    // Example widget tree produced:
    // Paragraph(
    //   Text(text: 'Hello World'),
    // );
  }
}
Simple document with paragraph, with bold and italic inline marks
Sample JSON
{
  "nodeType": "document",
  "content": [
    {
      "nodeType": "paragraph",
      "content": [
        {
          "nodeType": "text",
          "value": "Hello",
          "marks": [{ "type": "bold" }],
        },
        {
          "nodeType": "text",
          "value": " world!",
          "marks": [{ "type": "italic" }]
        }
      ]
    }
  ]
}
Sample Dart implementation
import 'package:contentful_rich_text/contentful_rich_text.dart';

// getting data from Contentful, must be implemented in your project
// document will be a dynamic variable JSON map matching the above JSON
var document = getContentfulJson();

class Demo extends StatelessWidget {

@override
  Widget build(BuildContext context) {
    return ContentfulRichText(document).documentToWidgetTree;
    // Example widget tree produced:
    // Paragraph(
    //   Text(
    //     text: '',
    //     children: <Widgets>[
    //       TextSpan(text: 'Hello', style: { fontWeight: FontWeight.bold }),
    //       TextSpan(text: ' world!', style: { fontStyle: FontStyle.italic }), 
    //     ],
    //   ),
    // );
  }
}
Simple document with paragraph, with bold and italic inline marks, using a custom renderer and mark

You can also pass custom renderers for both marks and nodes as an optional parameter like so:

Sample JSON
{
  "nodeType": "document",
  "data": {},
  "content": [
    {
      "nodeType": "paragraph",
      "data":{},
      "content": [
        {
          "nodeType": "text",
          "value": "Hello",
          "marks": [{ "type": "bold" }],
          "data": {}
        },
        {
          "nodeType": "text",
          "value": " world!",
          "marks": [{ "type": "italic" }],
          "data": {}
        }
      ]
    }
  ]
}
Sample Dart implementation
import 'package:contentful_rich_text/contentful_rich_text.dart';

// getting data from Contentful, must be implemented in your project
// document will be a dynamic variable JSON map matching the above JSON
var document = getContentfulJson();

var options = {
  renderMark: RenderMark defaultMarkRenderers = RenderMark({
    [MARKS.BOLD.value]: () => CustomBoldTextStyle, // returns TextStyle
  }),
  renderNode: RenderNode defaultNodeRenderers = RenderNode({
    [BLOCKS.PARAGRAPH.value]: (node, next) => CustomParagraph(next: next(node.content))
  })
}

class Demo extends StatelessWidget {

@override
  Widget build(BuildContext context) {
    return ContentfulRichText(document, options).documentToWidgetTree;
    // Example widget tree produced:
    // CustomParagraph(
    //   textStyle: CustomBoldTextStyle(),
    //   ...
    // );
  }
}
RenderNodes

RenderNodes are the various content nodes that Contentful sends for the Rich Text Widget. Blocks are block level items, and inlines are inline items.

Note: Not all of the nodes sent from Contentful are currently implemented. See below for the implementation status.

The renderNode keys should be the value of one of the following BLOCKS and INLINES properties as defined in contentful_rich_text/types:

  • BLOCKS

    • DOCUMENT
    • PARAGRAPH
    • HEADING_1
    • HEADING_2
    • HEADING_3
    • HEADING_4
    • HEADING_5
    • HEADING_6
    • UL_LIST // partially implemented, nested lists are not implemented yet
    • OL_LIST // partially implemented, nested lists are not implemented yet
    • LIST_ITEM
    • QUOTE // not implemented yet
    • HR
    • EMBEDDED_ENTRY // not implemented yet
    • EMBEDDED_ASSET // not implemented yet
  • INLINES

    • HYPERLINK
    • EMBEDDED_ENTRY (this is different from the BLOCKS.EMBEDDED_ENTRY)
    • ENTRY_HYPERLINK // not implemented yet
    • ASSET_HYPERLINK // not implemented yet
RenderMarks

RenderMarks are the various text styles that can be applied to text inline.

Note: Not all of the marks sent from Contentful are currently implemented. See below for the implementation status.

The renderMark keys should be the value of one of the following MARKS properties as defined in contentful_rich_text/types:

  • BOLD
  • ITALIC
  • UNDERLINE
  • CODE // not implemented yet

所属分类

依赖

依赖清单 3 项

源码仓库

使用此插件的项目

暂无相关数据。