Skip to content

Commit 077cf1d

Browse files
committed
Update documentation with advanced uses of custom image renders
1 parent d5fd222 commit 077cf1d

File tree

3 files changed

+11
-21
lines changed

3 files changed

+11
-21
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ The default image renders are:
425425
```dart
426426
final Map<ImageSourceMatcher, ImageRender> defaultImageRenders = {
427427
base64UriMatcher(): base64ImageRender(),
428+
assetUriMatcher(): assetImageRender(),
428429
networkSourceMatcher(extension: "svg"): svgNetworkImageRender(),
429430
networkSourceMatcher(): networkImageRender(),
430431
};
@@ -535,12 +536,16 @@ Widget html = Html(
535536
headers: {"Custom-Header": "some-value"},
536537
altWidget: (alt) => Text(alt),
537538
),
539+
(attr, _) => attr["src"] != null && attr["src"].startsWith("/wiki"):
540+
networkImageRender(
541+
mapUrl: (url) => "https://upload.wikimedia.org" + url),
538542
},
539543
);
540544
```
541545

542-
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.
546+
Above, there are three custom `networkSourceMatcher`s, which will be applied - in order - before the default implementations.
547+
548+
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 final render handles relative paths by rewriting them, specifically prefixing them with a base url. Note that the customizations of the previous custom renders do not apply. For example, the headers that the second render would apply are not applied in this third render.
544549

545550
2. Creating your own renders:
546551
```dart

example/lib/main.dart

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,6 @@ const htmlData = """
136136
<img alt='Empty source' src='' />
137137
<h3>Broken network image</h3>
138138
<img alt='Broken image' src='https://www.notgoogle.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' />
139-
<h3>Used inside a table</h3>
140-
<table>
141-
<tr>
142-
<td><img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></td>
143-
<td><img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></td>
144-
<td><img alt='Google' src='https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_92x30dp.png' /></td>
145-
</tr>
146-
</table>
147-
<h3>Video support:</h3>
148-
<video controls>
149-
<source src="https://www.w3schools.com/html/mov_bbb.mp4" />
150-
</video>
151-
<h3>Audio support:</h3>
152-
<audio controls>
153-
<source src="https://www.w3schools.com/html/mov_bbb.mp4" />
154-
</audio>
155-
<h3>IFrame support:</h3>
156-
<iframe src="https://google.com"></iframe>
157139
""";
158140

159141
class _MyHomePageState extends State<MyHomePage> {
@@ -177,9 +159,12 @@ class _MyHomePageState extends State<MyHomePage> {
177159
headers: {"Custom-Header": "some-value"},
178160
altWidget: (alt) => Text(alt),
179161
),
162+
// On relative paths starting with /wiki, prefix with a base url
180163
(attr, _) => attr["src"] != null && attr["src"].startsWith("/wiki"):
181164
networkImageRender(
182165
mapUrl: (url) => "https://upload.wikimedia.org" + url),
166+
// Custom placeholder image for broken links
167+
networkSourceMatcher(): networkImageRender(altWidget: (_) => FlutterLogo()),
183168
},
184169
onLinkTap: (url) {
185170
print("Opening $url...");

lib/image_render.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ ImageRender networkImageRender({
153153
},
154154
);
155155
} else if (snapshot.hasError) {
156-
return Text(_alt(attributes) ?? "",
156+
return altWidget?.call(_alt(attributes)) ?? Text(_alt(attributes) ?? "",
157157
style: context.style.generateTextStyle());
158158
} else {
159159
return new CircularProgressIndicator();

0 commit comments

Comments
 (0)