Skip to content

Commit 5d12366

Browse files
committed
Update customRender
1 parent b93e79b commit 5d12366

File tree

3 files changed

+51
-53
lines changed

3 files changed

+51
-53
lines changed

example/lib/main.dart

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -116,66 +116,57 @@ const htmlData = """
116116
<h3>Audio support:</h3>
117117
<h3>IFrame support:</h3>
118118
119-
120119
""";
121120

122121
class _MyHomePageState extends State<MyHomePage> {
123122
@override
124123
Widget build(BuildContext context) {
125124
return new Scaffold(
126125
appBar: AppBar(title: Text('flutter_html Example')),
127-
body: SafeArea(
128-
child: Row(
129-
children: <Widget>[
130-
Expanded(
131-
child: SingleChildScrollView(
132-
child: Html(
133-
data: htmlData,
134-
//Optional parameters:
135-
style: {
136-
"html": Style(
137-
backgroundColor: Colors.black,
138-
color: Colors.white,
139-
),
140-
"table": Style(
141-
backgroundColor: Color.fromARGB(0x50, 0xee, 0xee, 0xee),
142-
),
143-
"tr": Style(
144-
border: Border(bottom: BorderSide(color: Colors.grey)),
145-
),
146-
"th": Style(
147-
padding: EdgeInsets.all(6),
148-
backgroundColor: Colors.grey,
149-
),
150-
"td": Style(
151-
padding: EdgeInsets.all(6),
152-
),
153-
"var": Style(fontFamily: 'serif'),
154-
},
155-
customRender: {
156-
"flutter": (RenderContext context, Widget child, attributes) {
157-
return FlutterLogo(
158-
style: (attributes['horizontal'] != null)
159-
? FlutterLogoStyle.horizontal
160-
: FlutterLogoStyle.markOnly,
161-
textColor: context.style.color,
162-
size: context.style.fontSize.size * 5,
163-
);
164-
}
165-
},
166-
onLinkTap: (url) {
167-
print("Opening $url...");
168-
},
169-
onImageTap: (src) {
170-
print(src);
171-
},
172-
onImageError: (exception, stackTrace) {
173-
print(exception);
174-
},
175-
),
176-
),
126+
body: SingleChildScrollView(
127+
child: Html(
128+
data: htmlData,
129+
//Optional parameters:
130+
style: {
131+
"html": Style(
132+
backgroundColor: Colors.black,
133+
color: Colors.white,
134+
),
135+
"table": Style(
136+
backgroundColor: Color.fromARGB(0x50, 0xee, 0xee, 0xee),
137+
),
138+
"tr": Style(
139+
border: Border(bottom: BorderSide(color: Colors.grey)),
140+
),
141+
"th": Style(
142+
padding: EdgeInsets.all(6),
143+
backgroundColor: Colors.grey,
144+
),
145+
"td": Style(
146+
padding: EdgeInsets.all(6),
177147
),
178-
],
148+
"var": Style(fontFamily: 'serif'),
149+
},
150+
customRender: {
151+
"flutter": (RenderContext context, Widget child, attributes, _) {
152+
return FlutterLogo(
153+
style: (attributes['horizontal'] != null)
154+
? FlutterLogoStyle.horizontal
155+
: FlutterLogoStyle.markOnly,
156+
textColor: context.style.color,
157+
size: context.style.fontSize.size * 5,
158+
);
159+
},
160+
},
161+
onLinkTap: (url) {
162+
print("Opening $url...");
163+
},
164+
onImageTap: (src) {
165+
print(src);
166+
},
167+
onImageError: (exception, stackTrace) {
168+
print(exception);
169+
},
179170
),
180171
),
181172
);

lib/html_parser.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ import 'package:csslib/parser.dart' as cssparser;
1414

1515
typedef OnTap = void Function(String url);
1616
typedef CustomRender = Widget Function(
17-
RenderContext context, Widget child, Map<String, String> elementAttributes);
17+
RenderContext context,
18+
Widget parsedChild,
19+
Map<String, String> attributes,
20+
dom.Element element,
21+
);
1822

1923
class HtmlParser extends StatelessWidget {
2024
final String htmlData;
@@ -240,6 +244,7 @@ class HtmlParser extends StatelessWidget {
240244
[],
241245
),
242246
tree.attributes,
247+
tree.element,
243248
),
244249
),
245250
);

lib/src/styled_element.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class StyledElement {
2929
return MapEntry(key, value);
3030
});
3131

32+
dom.Element get element => _node;
33+
3234
@override
3335
String toString() {
3436
String selfData =

0 commit comments

Comments
 (0)