contentful_rich_text
Contentful 리치 텍스트 JSON 객체를 파싱하고 렌더링 가능한 Flutter 위젯 트리를 반환하는 리치 텍스트 렌더러
Contentful 리치 텍스트 JSON 객체를 파싱하고 렌더링 가능한 Flutter 위젯을 반환하는 리치 텍스트 렌더러
{"sdk":"flutter"}^1.18.0^2.0.0^6.1.5{"sdk":"flutter"}^4.0.0아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
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.
To use this plugin, add contentful_rich_text as a dependency in your pubspec.yaml file.
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.
{
"nodeType": "document",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "Hello world!",
"marks": []
}
]
}
]
}
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'),
// );
}
}
{
"nodeType": "document",
"content": [
{
"nodeType": "paragraph",
"content": [
{
"nodeType": "text",
"value": "Hello",
"marks": [{ "type": "bold" }],
},
{
"nodeType": "text",
"value": " world!",
"marks": [{ "type": "italic" }]
}
]
}
]
}
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 }),
// ],
// ),
// );
}
}
You can also pass custom renderers for both marks and nodes as an optional parameter like so:
{
"nodeType": "document",
"data": {},
"content": [
{
"nodeType": "paragraph",
"data":{},
"content": [
{
"nodeType": "text",
"value": "Hello",
"marks": [{ "type": "bold" }],
"data": {}
},
{
"nodeType": "text",
"value": " world!",
"marks": [{ "type": "italic" }],
"data": {}
}
]
}
]
}
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 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
DOCUMENTPARAGRAPHHEADING_1HEADING_2HEADING_3HEADING_4HEADING_5HEADING_6UL_LIST // partially implemented, nested lists are not implemented yetOL_LIST // partially implemented, nested lists are not implemented yetLIST_ITEMQUOTEHREMBEDDED_ENTRYEMBEDDED_ASSETINLINES
HYPERLINKEMBEDDED_ENTRY (this is different from the BLOCKS.EMBEDDED_ENTRY)ENTRY_HYPERLINKASSET_HYPERLINKRenderMarks 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:
BOLDITALICUNDERLINECODE