-
-
Notifications
You must be signed in to change notification settings - Fork 886
All About customRender
Version 1.0.0 reintroduces support for a customRender
function, which allows you to render elements however you want so that you can control every last pixel of your HTML-to-Widget app.
This page will attempt to give you the resources you need to make full use of customRender
for whatever your needs are.
The customRender
callback takes a Map<String, CustomRender>
like so:
Html(
data: ...,
customRender: {
"MyElement": (_, __, ___, ____) {
return MyWidget();
}
}
),
The previous example would be the most basic usage of customRender
. It uses none of the available parameters and simply returns a Widget
. The available parameters are defined in the CustomRender
according to this typedef (this can be found in lib/html_parser.dart
):
typedef CustomRender = Widget Function(
RenderContext context,
Widget parsedChild,
Map<String, String> attributes,
dom.Element element,
);
Explanation and examples of each of the parameters follow.
The context
contains some useful information about the current state of the tree where your element is being rendered. There are three parts of a RenderContext
:
-
BuildContext buildContext
: TheBuildContext
of theHtml
widget. -
HtmlParser parser
: This contains all the configuration information for theHtml
widget, including potentially useful values such asshrinkWrap
,onLinkTap
, andonImageError
. -
Style style
: This contains theStyle
of the current element. See theStyle
wiki page for more info.
Html(
data: "<button>Next Page</button>
customRender: {
"button": (context, _, __, ___) {
return MaterialButton(
onPressed: () {
Navigator.of(context.buildContext).pushNamed('/new-page');
},
child: Text("My Custom Button"),
);
},
},
),
Sometimes you only need to modify the way one element is displayed, but don't want to deal with parsing and rendering each of your custom element's children. In this case, you can use the parsedChild
parameter. The parsedChild
parameter gives you a Widget
with all of the custom element's children parsed by the default parser. It even supports nested customRender
ed Widgets!
Html(
data: "<button>Next Page</button>
customRender: {
"button": (context, child, _, __) {
return MaterialButton(
onPressed: () {
Navigator.of(context.buildContext).pushNamed('/new-page');
},
child: child,
);
},
},
),
This can be used to directly access the attributes of an HTML element if you don't want to deal with the dom.Element
object. All values are strings.
Html(
data: "Normal: <flutter></flutter><br />Horizontal: <flutter horizontal></flutter>"
customRender: {
"flutter": (context, child, attributes, _) {
return FlutterLogo(
style: (attributes['horizontal'] != null)
? FlutterLogoStyle.horizontal
: FlutterLogoStyle.markOnly,
textColor: context.style.color,
size: context.style.fontSize.size * 5,
);
},
},
),
This attribute can be used if you want to do everything yourself. It provides access to the dom.Element
node that customRender
is being run on (which in turn provides access to the entire DOM tree). Most basic use-cases probably won't need to worry about this parameter, but it can be helpful for doing more advanced custom elements.
Coming Soon!
Coming soon!