Skip to content

Commit df53f43

Browse files
authored
Merge pull request Sub6Resources#587 from vrtdev/feature/584-custom-render-tree-proposal
Pass element (tree) to custom render
2 parents b792d06 + 47e3931 commit df53f43

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,5 @@ modules.xml
149149
**/.flutter-plugins-dependencies
150150

151151
**/flutter_export_environment.sh
152+
153+
/example/ios/Flutter/Flutter.podspec

example/lib/main.dart

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_html/flutter_html.dart';
33
import 'package:flutter_html/image_render.dart';
4+
import 'package:flutter_html/src/layout_element.dart';
5+
import 'package:flutter_html/style.dart';
46

57
void main() => runApp(new MyApp());
68

@@ -245,7 +247,30 @@ class _MyHomePageState extends State<MyHomePage> {
245247
body: SingleChildScrollView(
246248
child: Html(
247249
data: htmlData,
248-
//Optional parameters:
250+
style: {
251+
"table": Style(
252+
backgroundColor: Color.fromARGB(0x50, 0xee, 0xee, 0xee),
253+
),
254+
"tr": Style(
255+
border: Border(bottom: BorderSide(color: Colors.grey)),
256+
),
257+
"th": Style(
258+
padding: EdgeInsets.all(6),
259+
backgroundColor: Colors.grey,
260+
),
261+
"td": Style(
262+
padding: EdgeInsets.all(6),
263+
alignment: Alignment.topLeft,
264+
),
265+
},
266+
customRender: {
267+
"table": (context, child) {
268+
return SingleChildScrollView(
269+
scrollDirection: Axis.horizontal,
270+
child: (context.tree as TableLayoutElement).toWidget(context),
271+
);
272+
}
273+
},
249274
customImageRenders: {
250275
networkSourceMatcher(domains: ["flutter.dev"]): (context, attributes, element) {
251276
return FlutterLogo(size: 36);

lib/html_parser.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ typedef OnMathError = Widget Function(
3030
typedef CustomRender = dynamic Function(
3131
RenderContext context,
3232
Widget parsedChild,
33-
Map<String, String> attributes,
34-
dom.Element? element,
3533
);
3634

3735
class HtmlParser extends StatelessWidget {
@@ -78,6 +76,7 @@ class HtmlParser extends StatelessWidget {
7876
RenderContext(
7977
buildContext: context,
8078
parser: this,
79+
tree: cleanedTree,
8180
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
8281
),
8382
cleanedTree,
@@ -94,6 +93,7 @@ class HtmlParser extends StatelessWidget {
9493
renderContext: RenderContext(
9594
buildContext: context,
9695
parser: this,
96+
tree: cleanedTree,
9797
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
9898
),
9999
);
@@ -242,6 +242,7 @@ class HtmlParser extends StatelessWidget {
242242
RenderContext newContext = RenderContext(
243243
buildContext: context.buildContext,
244244
parser: this,
245+
tree: tree,
245246
style: context.style.copyOnlyInherited(tree.style),
246247
);
247248

@@ -254,8 +255,6 @@ class HtmlParser extends StatelessWidget {
254255
shrinkWrap: context.parser.shrinkWrap,
255256
children: tree.children.map((tree) => parseTree(newContext, tree)).toList(),
256257
),
257-
tree.attributes,
258-
tree.element,
259258
);
260259
if (render != null) {
261260
assert(render is InlineSpan || render is Widget);
@@ -723,11 +722,13 @@ class HtmlParser extends StatelessWidget {
723722
class RenderContext {
724723
final BuildContext buildContext;
725724
final HtmlParser parser;
725+
final StyledElement tree;
726726
final Style style;
727727

728728
RenderContext({
729729
required this.buildContext,
730730
required this.parser,
731+
required this.tree,
731732
required this.style,
732733
});
733734
}

lib/src/layout_element.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ class TableLayoutElement extends LayoutElement {
3838
),
3939
width: style.width,
4040
height: style.height,
41-
child: _layoutCells(context),
41+
child: LayoutBuilder(builder: (_, constraints) => _layoutCells(context, constraints)),
4242
);
4343
}
4444

45-
Widget _layoutCells(RenderContext context) {
45+
Widget _layoutCells(RenderContext context, BoxConstraints constraints) {
4646
final rows = <TableRowLayoutElement>[];
4747
List<TrackSize> columnSizes = <TrackSize>[];
4848
for (var child in children) {
@@ -55,6 +55,10 @@ class TableLayoutElement extends LayoutElement {
5555
final colWidth = c.attributes["width"];
5656
return List.generate(span, (index) {
5757
if (colWidth != null && colWidth.endsWith("%")) {
58+
if (!constraints.hasBoundedWidth) {
59+
// In a horizontally unbounded container; always wrap content instead of applying flex
60+
return IntrinsicContentTrackSize();
61+
}
5862
final percentageSize = double.tryParse(
5963
colWidth.substring(0, colWidth.length - 1));
6064
return percentageSize != null && !percentageSize.isNaN

0 commit comments

Comments
 (0)