Skip to content

Commit 47e3931

Browse files
committed
Proposal for Sub6Resources#584 to add element (tree) to custom render, allowing to wrap the default implementation
1 parent 7e7ec9b commit 47e3931

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
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: 27 additions & 3 deletions
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

@@ -149,10 +151,32 @@ class _MyHomePageState extends State<MyHomePage> {
149151
body: SingleChildScrollView(
150152
child: Html(
151153
data: htmlData,
152-
//Optional parameters:
154+
style: {
155+
"table": Style(
156+
backgroundColor: Color.fromARGB(0x50, 0xee, 0xee, 0xee),
157+
),
158+
"tr": Style(
159+
border: Border(bottom: BorderSide(color: Colors.grey)),
160+
),
161+
"th": Style(
162+
padding: EdgeInsets.all(6),
163+
backgroundColor: Colors.grey,
164+
),
165+
"td": Style(
166+
padding: EdgeInsets.all(6),
167+
alignment: Alignment.topLeft,
168+
),
169+
},
170+
customRender: {
171+
"table": (context, child) {
172+
return SingleChildScrollView(
173+
scrollDirection: Axis.horizontal,
174+
child: (context.tree as TableLayoutElement).toWidget(context),
175+
);
176+
}
177+
},
153178
customImageRenders: {
154-
networkSourceMatcher(domains: ["flutter.dev"]):
155-
(context, attributes, element) {
179+
networkSourceMatcher(domains: ["flutter.dev"]): (context, attributes, element) {
156180
return FlutterLogo(size: 36);
157181
},
158182
networkSourceMatcher(domains: ["mydomain.com"]): networkImageRender(

lib/html_parser.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ typedef OnTap = void Function(
2525
typedef CustomRender = dynamic Function(
2626
RenderContext context,
2727
Widget parsedChild,
28-
Map<String, String> attributes,
29-
dom.Element? element,
3028
);
3129

3230
class HtmlParser extends StatelessWidget {
@@ -72,6 +70,7 @@ class HtmlParser extends StatelessWidget {
7270
RenderContext(
7371
buildContext: context,
7472
parser: this,
73+
tree: cleanedTree,
7574
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
7675
),
7776
cleanedTree,
@@ -88,6 +87,7 @@ class HtmlParser extends StatelessWidget {
8887
renderContext: RenderContext(
8988
buildContext: context,
9089
parser: this,
90+
tree: cleanedTree,
9191
style: Style.fromTextStyle(Theme.of(context).textTheme.bodyText2!),
9292
),
9393
);
@@ -236,6 +236,7 @@ class HtmlParser extends StatelessWidget {
236236
RenderContext newContext = RenderContext(
237237
buildContext: context.buildContext,
238238
parser: this,
239+
tree: tree,
239240
style: context.style.copyOnlyInherited(tree.style),
240241
);
241242

@@ -248,8 +249,6 @@ class HtmlParser extends StatelessWidget {
248249
shrinkWrap: context.parser.shrinkWrap,
249250
children: tree.children.map((tree) => parseTree(newContext, tree)).toList(),
250251
),
251-
tree.attributes,
252-
tree.element,
253252
);
254253
if (render != null) {
255254
assert(render is InlineSpan || render is Widget);
@@ -713,11 +712,13 @@ class HtmlParser extends StatelessWidget {
713712
class RenderContext {
714713
final BuildContext buildContext;
715714
final HtmlParser parser;
715+
final StyledElement tree;
716716
final Style style;
717717

718718
RenderContext({
719719
required this.buildContext,
720720
required this.parser,
721+
required this.tree,
721722
required this.style,
722723
});
723724
}

lib/src/layout_element.dart

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

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

0 commit comments

Comments
 (0)