搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。