feat: add image node widget in example

This commit is contained in:
Lucas.Xu 2022-07-12 18:39:21 +08:00
parent 5efa402582
commit e3c489612e
3 changed files with 79 additions and 4 deletions

View File

@ -28,6 +28,20 @@
"font-size": 30,
"content": "cccccccccccccccccccccc"
}
},
{
"type": "image",
"attributes": {
"image_src": "https://images.pexels.com/photos/12499889/pexels-photo-12499889.jpeg?fm=jpg&w=640&h=427"
}
},
{
"type": "text",
"attributes": {
"text-type": "heading1",
"font-size": 30,
"content": "dddddddddddddddddddd"
}
}
]
}

View File

@ -1,5 +1,6 @@
import 'dart:convert';
import 'package:example/plugin/image_node_widget.dart';
import 'package:example/plugin/text_node_widget.dart';
import 'package:flutter/material.dart';
import 'package:flowy_editor/flowy_editor.dart';
@ -58,9 +59,14 @@ class _MyHomePageState extends State<MyHomePage> {
void initState() {
super.initState();
renderPlugins.register(
renderPlugins
..register(
'text',
textNodeWidgetBuilder,
)
..register(
'image',
imageNodeWidgetBuilder,
);
}

View File

@ -0,0 +1,55 @@
import 'package:flowy_editor/flowy_editor.dart';
import 'package:flutter/material.dart';
NodeWidgetBuilder<Node> imageNodeWidgetBuilder =
(node, renderPlugins) => ImageNodeWidget(
node: node,
renderPlugins: renderPlugins,
);
class ImageNodeWidget extends BaseNodeWidget {
const ImageNodeWidget({
super.key,
required super.node,
required super.renderPlugins,
});
@override
State<ImageNodeWidget> createState() => _ImageNodeWidgetState();
}
class _ImageNodeWidgetState extends State<ImageNodeWidget> {
Node get node => widget.node;
String get src => node.attributes['image_src'] as String;
@override
Widget build(BuildContext context) {
final childWidget = renderChildren();
final image = Image.network(src);
if (childWidget != null) {
return Column(
children: [image, childWidget],
);
} else {
return image;
}
}
// manage children's render
Widget? renderChildren() {
if (node.children.isEmpty) {
return null;
}
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: node.children
.map(
(e) => widget.renderPlugins.buildWidgetWithNode(
e,
),
)
.toList(),
);
}
}