1
1
import 'dart:convert' ;
2
2
3
- import 'package:flutter/material.dart' ;
4
3
import 'package:flutter/gestures.dart' ;
5
- import 'package:html/parser .dart' as parser ;
4
+ import 'package:flutter/material .dart' ;
6
5
import 'package:html/dom.dart' as dom;
6
+ import 'package:html/parser.dart' as parser;
7
7
8
8
typedef CustomRender = Widget Function (dom.Node node, List <Widget > children);
9
9
typedef OnLinkTap = void Function (String url);
@@ -830,6 +830,7 @@ class HtmlOldParser extends StatelessWidget {
830
830
this .customRender,
831
831
this .blockSpacing,
832
832
this .html,
833
+ this .onImageError,
833
834
});
834
835
835
836
final double width;
@@ -838,6 +839,7 @@ class HtmlOldParser extends StatelessWidget {
838
839
final CustomRender customRender;
839
840
final double blockSpacing;
840
841
final String html;
842
+ final ImageErrorListener onImageError;
841
843
842
844
static const _supportedElements = [
843
845
"a" ,
@@ -1305,24 +1307,39 @@ class HtmlOldParser extends StatelessWidget {
1305
1307
),
1306
1308
);
1307
1309
case "img" :
1308
- if (node.attributes['src' ] != null ) {
1309
- if (node.attributes['src' ].startsWith ("data:image" ) &&
1310
- node.attributes['src' ].contains ("base64," )) {
1311
- return Image .memory (base64
1312
- .decode (node.attributes['src' ].split ("base64," )[1 ].trim ()));
1313
- }
1314
- return Image .network (node.attributes['src' ]);
1315
- } else if (node.attributes['alt' ] != null ) {
1316
- //Temp fix for https://github.com/flutter/flutter/issues/736
1317
- if (node.attributes['alt' ].endsWith (" " )) {
1318
- return Container (
1319
- padding: EdgeInsets .only (right: 2.0 ),
1320
- child: Text (node.attributes['alt' ]));
1321
- } else {
1322
- return Text (node.attributes['alt' ]);
1323
- }
1324
- }
1325
- return Container ();
1310
+ return Builder (
1311
+ builder: (BuildContext context) {
1312
+ if (node.attributes['src' ] != null ) {
1313
+ if (node.attributes['src' ].startsWith ("data:image" ) &&
1314
+ node.attributes['src' ].contains ("base64," )) {
1315
+ precacheImage (
1316
+ MemoryImage (base64.decode (
1317
+ node.attributes['src' ].split ("base64," )[1 ].trim ())),
1318
+ context,
1319
+ onError: onImageError,
1320
+ );
1321
+ return Image .memory (base64.decode (
1322
+ node.attributes['src' ].split ("base64," )[1 ].trim ()));
1323
+ }
1324
+ precacheImage (
1325
+ NetworkImage (node.attributes['src' ]),
1326
+ context,
1327
+ onError: onImageError,
1328
+ );
1329
+ return Image .network (node.attributes['src' ]);
1330
+ } else if (node.attributes['alt' ] != null ) {
1331
+ //Temp fix for https://github.com/flutter/flutter/issues/736
1332
+ if (node.attributes['alt' ].endsWith (" " )) {
1333
+ return Container (
1334
+ padding: EdgeInsets .only (right: 2.0 ),
1335
+ child: Text (node.attributes['alt' ]));
1336
+ } else {
1337
+ return Text (node.attributes['alt' ]);
1338
+ }
1339
+ }
1340
+ return Container ();
1341
+ },
1342
+ );
1326
1343
case "ins" :
1327
1344
return DefaultTextStyle .merge (
1328
1345
child: Wrap (
0 commit comments