Skip to content

Commit f1fe495

Browse files
committed
Fix image alt attribute (fixes Sub6Resources#96)
1 parent 247a8f3 commit f1fe495

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## [0.11.0] - September 6, 2019:
22

33
* Make it so `width=100%` doesn't throw error. Fixes [#118](https://github.com/Sub6Resources/flutter_html/issues/118).
4-
* You can now set width and/or height in `ImageProperties` to negative to ignore the `width` and/or `height` values from the html.
4+
* You can now set width and/or height in `ImageProperties` to negative to ignore the `width` and/or `height` values from the html. Fixes [#97](https://github.com/Sub6Resources/flutter_html/issues/97)
5+
* The `img` `alt` property now renders correctly when the image fails to load and with the correct style. Fixes [#96](https://github.com/Sub6Resources/flutter_html/issues/96)
56

67
## [0.10.4] - June 22, 2019:
78

lib/rich_text_parser.dart

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:convert';
2+
import 'dart:io';
23

34
import 'package:flutter/gestures.dart';
45
import 'package:flutter/material.dart';
@@ -753,7 +754,7 @@ class HtmlRichTextParser extends StatelessWidget {
753754
),
754755
),
755756
buildContext,
756-
onError: onImageError,
757+
onError: onImageError ?? (_,__) {},
757758
);
758759
parseContext.rootWidgetList.add(GestureDetector(
759760
child: Image.memory(
@@ -788,11 +789,28 @@ class HtmlRichTextParser extends StatelessWidget {
788789
precacheImage(
789790
NetworkImage(node.attributes['src']),
790791
buildContext,
791-
onError: onImageError,
792+
onError: onImageError ?? (_,__) {},
792793
);
793794
parseContext.rootWidgetList.add(GestureDetector(
794795
child: Image.network(
795796
node.attributes['src'],
797+
frameBuilder: (context, child, frame, _) {
798+
if (node.attributes['alt'] != null && frame == null) {
799+
return BlockText(
800+
child: RichText(
801+
textAlign: TextAlign.center,
802+
text: TextSpan(
803+
text: node.attributes['alt'],
804+
style: nextContext.childStyle,
805+
),
806+
)
807+
);
808+
}
809+
if (frame != null) {
810+
return child;
811+
}
812+
return Container();
813+
},
796814
width: (width ?? -1) > 0? width: null,
797815
height: (height ?? -1) > 0? height: null,
798816
scale: imageProperties?.scale ?? 1.0,
@@ -819,19 +837,6 @@ class HtmlRichTextParser extends StatelessWidget {
819837
},
820838
));
821839
}
822-
if (node.attributes['alt'] != null) {
823-
parseContext.rootWidgetList.add(BlockText(
824-
margin:
825-
EdgeInsets.symmetric(horizontal: 0.0, vertical: 10.0),
826-
padding: EdgeInsets.all(0.0),
827-
child: RichText(
828-
textAlign: TextAlign.center,
829-
text: TextSpan(
830-
text: node.attributes['alt'],
831-
style: nextContext.childStyle,
832-
children: <TextSpan>[],
833-
))));
834-
}
835840
}
836841
}
837842
break;

0 commit comments

Comments
 (0)