Skip to content

Commit 1c7b9e2

Browse files
committed
Custom render refactor suggestion
1 parent d3f37f8 commit 1c7b9e2

File tree

6 files changed

+32
-38
lines changed

6 files changed

+32
-38
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ A powerful API that allows you to customize everything when rendering a specific
272272

273273
`CustomRenderMatcher` is a function that requires a `bool` to be returned. It exposes the `RenderContext` which provides `BuildContext` and access to the HTML tree.
274274

275-
The `CustomRender` class has two constructors: `CustomRender.fromWidget()` and `CustomRender.fromInlineSpan()`. Both require a `<Widget/InlineSpan> Function(RenderContext, Function())`. The `Function()` argument is a function that will provide you with the element's children when needed.
275+
The `CustomRender` class has two constructors: `CustomRender.widget()` and `CustomRender.inlineSpan()`. Both require a `<Widget/InlineSpan> Function(RenderContext, Function())`. The `Function()` argument is a function that will provide you with the element's children when needed.
276276

277277
To use this API, create a matching function and an instance of `CustomRender`.
278278

@@ -289,8 +289,8 @@ Widget html = Html(
289289
<flutter horizontal></flutter>
290290
""",
291291
customRender: {
292-
birdMatcher(): CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) => TextSpan(text: "🐦")),
293-
flutterMatcher(): CustomRender.fromWidget(widget: (context, buildChildren) => FlutterLogo(
292+
birdMatcher(): CustomRender.inlineSpan(inlineSpan: (context, buildChildren) => TextSpan(text: "🐦")),
293+
flutterMatcher(): CustomRender.widget(widget: (context, buildChildren) => FlutterLogo(
294294
style: (context.tree.element!.attributes['horizontal'] != null)
295295
? FlutterLogoStyle.horizontal
296296
: FlutterLogoStyle.markOnly,
@@ -321,7 +321,7 @@ Widget html = Html(
321321
</table>
322322
""",
323323
customRender: {
324-
tableMatcher(): CustomRender.fromWidget(widget: (context, child) {
324+
tableMatcher(): CustomRender.widget(widget: (context, child) {
325325
return SingleChildScrollView(
326326
scrollDirection: Axis.horizontal,
327327
child: (context.tree as TableLayoutElement).toWidget(context),
@@ -348,7 +348,7 @@ Widget html = Html(
348348
<iframe src="https://www.youtube.com/embed/tgbNymZ7vqY"></iframe>
349349
""",
350350
customRender: {
351-
iframeYT(): CustomRender.fromWidget(widget: (context, buildChildren) {
351+
iframeYT(): CustomRender.widget(widget: (context, buildChildren) {
352352
double? width = double.tryParse(context.tree.attributes['width'] ?? "");
353353
double? height = double.tryParse(context.tree.attributes['height'] ?? "");
354354
return Container(
@@ -368,7 +368,7 @@ Widget html = Html(
368368
),
369369
);
370370
}),
371-
iframeOther(): CustomRender.fromWidget(widget: (context, buildChildren) {
371+
iframeOther(): CustomRender.widget(widget: (context, buildChildren) {
372372
double? width = double.tryParse(context.tree.attributes['width'] ?? "");
373373
double? height = double.tryParse(context.tree.attributes['height'] ?? "");
374374
return Container(
@@ -384,7 +384,7 @@ Widget html = Html(
384384
),
385385
);
386386
}),
387-
iframeNull(): CustomRender.fromWidget(widget: (context, buildChildren) => Container(height: 0, width: 0)),
387+
iframeNull(): CustomRender.widget(widget: (context, buildChildren) => Container(height: 0, width: 0)),
388388
}
389389
);
390390
@@ -818,7 +818,7 @@ Then, use the `customRender` parameter to add the widget to render Tex. It could
818818
Widget htmlWidget = Html(
819819
data: r"""<tex>i\hbar\frac{\partial}{\partial t}\Psi(\vec x,t) = -\frac{\hbar}{2m}\nabla^2\Psi(\vec x,t)+ V(\vec x)\Psi(\vec x,t)</tex>""",
820820
customRender: {
821-
texMatcher(): CustomRender.fromWidget(widget: (context, buildChildren) => Math.tex(
821+
texMatcher(): CustomRender.widget(widget: (context, buildChildren) => Math.tex(
822822
context.tree.element?.innerHtml ?? '',
823823
mathStyle: MathStyle.display,
824824
textStyle: context.style.generateTextStyle(),

example/lib/main.dart

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ class _MyHomePageState extends State<MyHomePage> {
270270
},
271271
tagsList: Html.tags..addAll(["tex", "bird", "flutter"]),
272272
customRenders: {
273-
texMatcher(): CustomRender.fromWidget(widget: (context, buildChildren) => Math.tex(
273+
tagMatcher("tex"): CustomRender.widget(widget: (context, buildChildren) => Math.tex(
274274
context.tree.element?.innerHtml ?? '',
275275
mathStyle: MathStyle.display,
276276
textStyle: context.style.generateTextStyle(),
@@ -282,15 +282,15 @@ class _MyHomePageState extends State<MyHomePage> {
282282
}
283283
},
284284
)),
285-
birdMatcher(): CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) => TextSpan(text: "🐦")),
286-
flutterMatcher(): CustomRender.fromWidget(widget: (context, buildChildren) => FlutterLogo(
285+
tagMatcher("bird"): CustomRender.inlineSpan(inlineSpan: (context, buildChildren) => TextSpan(text: "🐦")),
286+
tagMatcher("flutter"): CustomRender.widget(widget: (context, buildChildren) => FlutterLogo(
287287
style: (context.tree.element!.attributes['horizontal'] != null)
288288
? FlutterLogoStyle.horizontal
289289
: FlutterLogoStyle.markOnly,
290290
textColor: context.style.color!,
291291
size: context.style.fontSize!.size! * 5,
292292
)),
293-
tableMatcher(): CustomRender.fromWidget(widget: (context, buildChildren) => SingleChildScrollView(
293+
tagMatcher("table"): CustomRender.widget(widget: (context, buildChildren) => SingleChildScrollView(
294294
scrollDirection: Axis.horizontal,
295295
child: (context.tree as TableLayoutElement).toWidget(context),
296296
)),
@@ -336,11 +336,3 @@ class _MyHomePageState extends State<MyHomePage> {
336336
);
337337
}
338338
}
339-
340-
CustomRenderMatcher tableMatcher() => (context) => context.tree.element?.localName == 'table';
341-
342-
CustomRenderMatcher texMatcher() => (context) => context.tree.element?.localName == 'tex';
343-
344-
CustomRenderMatcher birdMatcher() => (context) => context.tree.element?.localName == 'bird';
345-
346-
CustomRenderMatcher flutterMatcher() => (context) => context.tree.element?.localName == 'flutter';

example/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: example
22
description: flutter_html example app.
3-
3+
publish_to: none
44
version: 1.0.0+1
55

66
environment:

lib/custom_render.dart

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ import 'package:flutter_html/src/utils.dart';
77

88
typedef CustomRenderMatcher = bool Function(RenderContext context);
99

10-
CustomRenderMatcher blockElementMatcher() => (context) {
11-
return context.tree.style.display == Display.BLOCK
12-
&& context.tree.children.isNotEmpty;
10+
CustomRenderMatcher tagMatcher(String tag) => (context) {
11+
return context.tree.element?.localName == tag;
1312
};
1413

14+
CustomRenderMatcher blockElementMatcher() => (context) {
15+
return context.tree.style.display == Display.BLOCK &&
16+
(context.tree.children.isNotEmpty || context.tree.element?.localName == "hr");
17+
};
18+
1519
CustomRenderMatcher listElementMatcher() => (context) {
1620
return context.tree.style.display == Display.LIST_ITEM;
1721
};
@@ -45,11 +49,11 @@ class CustomRender {
4549
final InlineSpan Function(RenderContext, List<InlineSpan> Function())? inlineSpan;
4650
final Widget Function(RenderContext, List<InlineSpan> Function())? widget;
4751

48-
CustomRender.fromInlineSpan({
52+
CustomRender.inlineSpan({
4953
required this.inlineSpan,
5054
}) : widget = null;
5155

52-
CustomRender.fromWidget({
56+
CustomRender.widget({
5357
required this.widget,
5458
}) : inlineSpan = null;
5559
}
@@ -59,14 +63,14 @@ class SelectableCustomRender extends CustomRender {
5963

6064
SelectableCustomRender.fromTextSpan({
6165
required this.textSpan,
62-
}) : super.fromInlineSpan(inlineSpan: null);
66+
}) : super.inlineSpan(inlineSpan: null);
6367
}
6468

6569
CustomRender blockElementRender({
6670
Style? style,
6771
Widget? child,
68-
List<InlineSpan>? children}) =>
69-
CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) {
72+
List<InlineSpan>? children,}) =>
73+
CustomRender.inlineSpan(inlineSpan: (context, buildChildren) {
7074
if (context.parser.selectable) {
7175
return TextSpan(
7276
style: context.style.generateTextStyle(),
@@ -115,7 +119,7 @@ CustomRender listElementRender({
115119
Style? style,
116120
Widget? child,
117121
List<InlineSpan>? children}) =>
118-
CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) =>
122+
CustomRender.inlineSpan(inlineSpan: (context, buildChildren) =>
119123
WidgetSpan(
120124
child: ContainerSpan(
121125
key: context.key,
@@ -164,18 +168,18 @@ CustomRender listElementRender({
164168
));
165169

166170
CustomRender replacedElementRender({PlaceholderAlignment? alignment, TextBaseline? baseline, Widget? child}) =>
167-
CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) => WidgetSpan(
171+
CustomRender.inlineSpan(inlineSpan: (context, buildChildren) => WidgetSpan(
168172
alignment: alignment ?? (context.tree as ReplacedElement).alignment,
169173
baseline: baseline ?? TextBaseline.alphabetic,
170174
child: child ?? (context.tree as ReplacedElement).toWidget(context)!,
171175
));
172176

173177
CustomRender textContentElementRender({String? text}) =>
174-
CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) =>
178+
CustomRender.inlineSpan(inlineSpan: (context, buildChildren) =>
175179
TextSpan(text: text ?? (context.tree as TextContentElement).text));
176180

177181
CustomRender interactableElementRender({List<InlineSpan>? children}) =>
178-
CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) => TextSpan(
182+
CustomRender.inlineSpan(inlineSpan: (context, buildChildren) => TextSpan(
179183
children: children ?? (context.tree as InteractableElement).children
180184
.map((tree) => context.parser.parseTree(context, tree))
181185
.map((childSpan) {
@@ -185,15 +189,15 @@ CustomRender interactableElementRender({List<InlineSpan>? children}) =>
185189
));
186190

187191
CustomRender layoutElementRender({Widget? child}) =>
188-
CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) => WidgetSpan(
192+
CustomRender.inlineSpan(inlineSpan: (context, buildChildren) => WidgetSpan(
189193
child: child ?? (context.tree as LayoutElement).toWidget(context)!,
190194
));
191195

192196
CustomRender verticalAlignRender({
193197
double? verticalOffset,
194198
Style? style,
195199
List<InlineSpan>? children}) =>
196-
CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) => WidgetSpan(
200+
CustomRender.inlineSpan(inlineSpan: (context, buildChildren) => WidgetSpan(
197201
child: Transform.translate(
198202
key: context.key,
199203
offset: Offset(0, verticalOffset ?? _getVerticalOffset(context.tree)),
@@ -209,7 +213,7 @@ CustomRender verticalAlignRender({
209213
));
210214

211215
CustomRender fallbackRender({Style? style, List<InlineSpan>? children}) =>
212-
CustomRender.fromInlineSpan(inlineSpan: (context, buildChildren) => TextSpan(
216+
CustomRender.inlineSpan(inlineSpan: (context, buildChildren) => TextSpan(
213217
style: style?.generateTextStyle() ?? context.style.generateTextStyle(),
214218
children: context.tree.children
215219
.expand((tree) => [

lib/image_render.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import 'dart:convert';
44
import 'package:flutter/material.dart';
55
import 'package:flutter_html/html_parser.dart';
66
import 'package:flutter_svg/flutter_svg.dart';
7-
import 'package:flutter_svg/parser.dart';
87
import 'package:html/dom.dart' as dom;
98

109
typedef ImageSourceMatcher = bool Function(

lib/src/utils.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'dart:convert';
22
import 'dart:math';
33

4-
import 'package:flutter/gestures.dart';
54
import 'package:flutter/material.dart';
65

76
Map<String, String> namedColors = {

0 commit comments

Comments
 (0)