mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: pass BuildContext to NodeWidgetBuilder
This commit is contained in:
parent
88f73bfbd4
commit
05786d1255
@ -88,8 +88,11 @@ class _MyHomePageState extends State<MyHomePage> {
|
|||||||
} else {
|
} else {
|
||||||
final data = Map<String, Object>.from(json.decode(snapshot.data!));
|
final data = Map<String, Object>.from(json.decode(snapshot.data!));
|
||||||
final stateTree = StateTree.fromJson(data);
|
final stateTree = StateTree.fromJson(data);
|
||||||
return renderPlugins.buildWidgetWithNode(
|
return renderPlugins.buildWidget(
|
||||||
stateTree.root,
|
NodeWidgetContext(
|
||||||
|
buildContext: context,
|
||||||
|
node: stateTree.root,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -8,14 +8,26 @@ class ImageNodeBuilder extends NodeWidgetBuilder {
|
|||||||
String get src => node.attributes['image_src'] as String;
|
String get src => node.attributes['image_src'] as String;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build() {
|
Widget build(BuildContext buildContext) {
|
||||||
final childrenWidget = buildChildren();
|
|
||||||
final image = Image.network(src);
|
final image = Image.network(src);
|
||||||
if (childrenWidget != null) {
|
Widget? children;
|
||||||
|
if (node.children.isNotEmpty) {
|
||||||
|
children = Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: node.children
|
||||||
|
.map(
|
||||||
|
(e) => renderPlugins.buildWidget(
|
||||||
|
NodeWidgetContext(buildContext: buildContext, node: e),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (children != null) {
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
image,
|
image,
|
||||||
childrenWidget,
|
children,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,19 +8,33 @@ class TextNodeBuilder extends NodeWidgetBuilder {
|
|||||||
String get content => node.attributes['content'] as String;
|
String get content => node.attributes['content'] as String;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build() {
|
Widget build(BuildContext buildContext) {
|
||||||
final childrenWidget = buildChildren();
|
|
||||||
final richText = SelectableText.rich(
|
final richText = SelectableText.rich(
|
||||||
TextSpan(
|
TextSpan(
|
||||||
text: node.attributes['content'] as String,
|
text: node.attributes['content'] as String,
|
||||||
style: node.attributes.toTextStyle(),
|
style: node.attributes.toTextStyle(),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
if (childrenWidget != null) {
|
|
||||||
|
Widget? children;
|
||||||
|
if (node.children.isNotEmpty) {
|
||||||
|
children = Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: node.children
|
||||||
|
.map(
|
||||||
|
(e) => renderPlugins.buildWidget(
|
||||||
|
NodeWidgetContext(buildContext: buildContext, node: e),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (children != null) {
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
children: [
|
||||||
richText,
|
richText,
|
||||||
childrenWidget,
|
children,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
@ -9,21 +9,9 @@ class NodeWidgetBuilder<T extends Node> {
|
|||||||
|
|
||||||
NodeWidgetBuilder.create({required this.node, required this.renderPlugins});
|
NodeWidgetBuilder.create({required this.node, required this.renderPlugins});
|
||||||
|
|
||||||
Widget call() => build();
|
Widget call(BuildContext buildContext) => build(buildContext);
|
||||||
Widget build() => throw UnimplementedError();
|
|
||||||
Widget? buildChildren() {
|
|
||||||
if (node.children.isEmpty) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// default layout
|
/// Render the current [Node]
|
||||||
return Column(
|
/// and the layout style of [Node.Children].
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
Widget build(BuildContext buildContext) => throw UnimplementedError();
|
||||||
children: node.children
|
|
||||||
.map(
|
|
||||||
(e) => renderPlugins.buildWidgetWithNode(e),
|
|
||||||
)
|
|
||||||
.toList(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,12 @@ import 'package:flutter/material.dart';
|
|||||||
import '../document/node.dart';
|
import '../document/node.dart';
|
||||||
import 'node_widget_builder.dart';
|
import 'node_widget_builder.dart';
|
||||||
|
|
||||||
|
class NodeWidgetContext {
|
||||||
|
BuildContext buildContext;
|
||||||
|
Node node;
|
||||||
|
NodeWidgetContext({required this.buildContext, required this.node});
|
||||||
|
}
|
||||||
|
|
||||||
typedef NodeWidgetBuilderF<T extends Node, A extends NodeWidgetBuilder> = A
|
typedef NodeWidgetBuilderF<T extends Node, A extends NodeWidgetBuilder> = A
|
||||||
Function({
|
Function({
|
||||||
required T node,
|
required T node,
|
||||||
@ -28,13 +34,15 @@ class RenderPlugins {
|
|||||||
nodeWidgetBuilders.removeWhere((key, _) => key == name);
|
nodeWidgetBuilders.removeWhere((key, _) => key == name);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget buildWidgetWithNode(Node node) {
|
Widget buildWidget(NodeWidgetContext context) {
|
||||||
final nodeWidgetBuilder = _nodeWidgetBuilder(node.type);
|
final nodeWidgetBuilder = _nodeWidgetBuilder(context.node.type);
|
||||||
return nodeWidgetBuilder(node: node, renderPlugins: this)();
|
return nodeWidgetBuilder(node: context.node, renderPlugins: this)(
|
||||||
|
context.buildContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeWidgetBuilderF _nodeWidgetBuilder(String name) {
|
NodeWidgetBuilderF _nodeWidgetBuilder(String name) {
|
||||||
assert(nodeWidgetBuilders.containsKey(name));
|
assert(nodeWidgetBuilders.containsKey(name),
|
||||||
|
'Could not query the builder with this $name');
|
||||||
return nodeWidgetBuilders[name]!;
|
return nodeWidgetBuilders[name]!;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user