You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -140,6 +148,7 @@ Below, you will find brief descriptions of the parameters the`Html` widget accep
140
148
|`blacklistedElements`| A list of elements the `Html` widget should not render. The list should contain the tags of the HTML elements you wish to blacklist. |
141
149
|`style`| A powerful API that allows you to customize the style that should be used when rendering a specific HTMl tag. |
142
150
|`navigationDelegateForIframe`| Allows you to set the `NavigationDelegate` for the `WebView`s of all the iframes rendered by the `Html` widget. |
151
+
|`customImageRender`| A powerful API that allows you to fully customize how images are loaded. |
143
152
144
153
### Data:
145
154
@@ -267,7 +276,7 @@ Widget html = Html(
267
276
}
268
277
}
269
278
}
270
-
),
279
+
);
271
280
```
272
281
</details>
273
282
@@ -405,6 +414,175 @@ Widget html = Html(
405
414
);
406
415
```
407
416
417
+
### customImageRender:
418
+
419
+
A powerful API that allows you to customize what the `Html` widget does when rendering an image, down to the most minute detail.
420
+
421
+
`customImageRender` accepts a `Map<ImageSourceMatcher, ImageRender>`. `ImageSourceMatcher` provides the matching function, while `ImageRender` provides the widget to be rendered.
422
+
423
+
The default image renders are:
424
+
425
+
```dart
426
+
final Map<ImageSourceMatcher, ImageRender> defaultImageRenders = {
See [the source code](https://github.com/Sub6Resources/flutter_html/blob/master/lib/image_render.dart) for details on how these are implemented.
434
+
435
+
When setting `customImageRenders`, the package will prioritize the custom renders first, while the default ones are used as a fallback.
436
+
437
+
Note: Order is very important when you set `customImageRenders`. The more specific your `ImageSourceMatcher`, the higher up in the `customImageRender` list it should be.
438
+
439
+
#### typedef ImageSourceMatcher
440
+
441
+
This is type defined as a function that passes the attributes as a `Map<String, String>` and the DOM element as `dom.Element`. This type is used to define how an image should be matched i.e. whether the package should override the default rendering method and instead use your custom implementation.
In the above example, the matcher checks whether the image's `src` either starts with "data:image" or contains "base64,", since these indicate an image in base64 format.
452
+
453
+
You can also declare your own variables in the function itself, which would look like this:
454
+
455
+
```dart
456
+
ImageSourceMatcher networkSourceMatcher({
457
+
/// all three are optional, you don't need to have these in the function
In the above example, the possible schemas are checked against the scheme of the `src`, and optionally the domains and extensions are also checked. This implementation allows for extremely granular control over what images are matched, and could even be changed on the fly with a variable.
471
+
472
+
#### typedef ImageRender
473
+
474
+
This is a type defined as a function that passes the attributes of the image as a `Map<String, String>`, the current [`RenderContext`](https://github.com/Sub6Resources/flutter_html/wiki/All-About-customRender#rendercontext-context), and the DOM element as `dom.Element`. This type is used to define the widget that should be rendered when used in conjunction with an `ImageSourceMatcher`.
Above, there are two `networkSourceMatcher`s. One is overriden to only apply to images on the URL `flutter.dev`, while the other covers the rest of the cases.
543
+
When an image with URL `flutter.dev` is detected, rather than displaying the image, the render will display the flutter logo. If the image is any other image, it keeps the default widget, but just sets the headers and the alt text in case that image happens to be broken.
The above example has a matcher that checks for either a class or an id, and then returns two different widgets based on whether a class was matched or an id was matched.
583
+
584
+
The sky is the limit when using the custom image renders. You can make it as granular as you want, or as all-encompassing as you want, and you have full control of everything. Plus you get the package's style parsing to use in your custom widgets, so your code looks neat and readable!
585
+
408
586
## Rendering Reference
409
587
410
588
This section will describe how certain HTML elements are rendered by this package, so you can evaluate how your HTML will be rendered and structure it accordingly.
0 commit comments