Skip to content

Commit 7289b18

Browse files
author
Andrei
committed
Added onTap support, ImageProperties and 'alt' element fix
- Added onTap support for Image widgets that are parsed by the richTextParser - Added ImageProperties to the richTextParser - Fixed 'alt' HTML element - it was not working before due to the if..else if.. confusion
1 parent fdaca87 commit 7289b18

File tree

1 file changed

+58
-15
lines changed

1 file changed

+58
-15
lines changed

lib/html_parser.dart

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import 'dart:convert';
2-
2+
import 'image_properties.dart';
33
import 'package:flutter/gestures.dart';
44
import 'package:flutter/material.dart';
55
import 'package:html/dom.dart' as dom;
@@ -12,7 +12,7 @@ typedef CustomTextStyle = TextStyle Function(
1212
);
1313
typedef CustomEdgeInsets = EdgeInsets Function(dom.Node node);
1414
typedef OnLinkTap = void Function(String url);
15-
15+
typedef OnImageTap = void Function();
1616
const OFFSET_TAGS_FONT_SIZE_FACTOR =
1717
0.7; //The ratio of the parent font for each of the offset tags: sup or sub
1818

@@ -157,6 +157,8 @@ class HtmlRichTextParser extends StatelessWidget {
157157
color: Colors.blueAccent,
158158
decorationColor: Colors.blueAccent,
159159
),
160+
this.imageProperties,
161+
this.onImageTap,
160162
});
161163

162164
final double indentSize = 10.0;
@@ -169,6 +171,8 @@ class HtmlRichTextParser extends StatelessWidget {
169171
final CustomEdgeInsets customEdgeInsets;
170172
final ImageErrorListener onImageError;
171173
final TextStyle linkStyle;
174+
final ImageProperties imageProperties;
175+
final OnImageTap onImageTap;
172176

173177
// style elements set a default style
174178
// for all child nodes
@@ -654,26 +658,65 @@ class HtmlRichTextParser extends StatelessWidget {
654658
buildContext,
655659
onError: onImageError,
656660
);
657-
parseContext.rootWidgetList.add(Image.memory(base64.decode(
658-
node.attributes['src'].split("base64,")[1].trim())));
661+
parseContext.rootWidgetList.add(InkWell(
662+
child: Image.memory(
663+
base64.decode(node.attributes['src'].split("base64,")[1].trim()),
664+
width: imageProperties?.width,
665+
height: imageProperties?.height,
666+
scale: imageProperties?.scale ?? 1.0,
667+
matchTextDirection: imageProperties?.matchTextDirection ?? false,
668+
centerSlice: imageProperties?.centerSlice,
669+
gaplessPlayback: imageProperties?.gaplessPlayback ?? false,
670+
filterQuality: imageProperties?.filterQuality ?? FilterQuality.low,
671+
alignment: imageProperties?.alignment ?? Alignment.center,
672+
colorBlendMode: imageProperties?.colorBlendMode,
673+
fit: imageProperties?.fit,
674+
color: imageProperties?.color,
675+
repeat: imageProperties?.repeat ?? ImageRepeat.noRepeat,
676+
semanticLabel: imageProperties?.semanticLabel,
677+
excludeFromSemantics: (imageProperties?.semanticLabel == null) ? true : false,
678+
),
679+
onTap: onImageTap,
680+
));
659681
} else {
660682
precacheImage(
661683
NetworkImage(node.attributes['src']),
662684
buildContext,
663685
onError: onImageError,
664686
);
665-
parseContext.rootWidgetList
666-
.add(Image.network(node.attributes['src']));
687+
parseContext.rootWidgetList.add(InkWell(
688+
child: Image.network(
689+
node.attributes['src'],
690+
width: imageProperties?.width,
691+
height: imageProperties?.height,
692+
scale: imageProperties?.scale ?? 1.0,
693+
matchTextDirection: imageProperties?.matchTextDirection ?? false,
694+
centerSlice: imageProperties?.centerSlice,
695+
gaplessPlayback: imageProperties?.gaplessPlayback ?? false,
696+
filterQuality: imageProperties?.filterQuality ?? FilterQuality.low,
697+
alignment: imageProperties?.alignment ?? Alignment.center,
698+
colorBlendMode: imageProperties?.colorBlendMode,
699+
fit: imageProperties?.fit,
700+
color: imageProperties?.color,
701+
repeat: imageProperties?.repeat ?? ImageRepeat.noRepeat,
702+
semanticLabel: imageProperties?.semanticLabel,
703+
excludeFromSemantics: (imageProperties?.semanticLabel == null) ? true : false,
704+
),
705+
onTap: onImageTap,
706+
));
707+
}
708+
if (node.attributes['alt'] != null) {
709+
parseContext.rootWidgetList.add(BlockText(
710+
margin: EdgeInsets.symmetric(horizontal: 0.0, vertical: 10.0),
711+
padding: EdgeInsets.all(0.0),
712+
child: RichText(
713+
textAlign: TextAlign.center,
714+
text: TextSpan(
715+
text: node.attributes['alt'],
716+
style: nextContext.childStyle,
717+
children: <TextSpan>[],
718+
))));
667719
}
668-
} else if (node.attributes['alt'] != null) {
669-
parseContext.rootWidgetList.add(BlockText(
670-
margin: EdgeInsets.symmetric(horizontal: 0.0, vertical: 10.0),
671-
padding: EdgeInsets.all(0.0),
672-
child: RichText(
673-
text: TextSpan(
674-
text: node.attributes['alt'],
675-
children: <TextSpan>[],
676-
))));
677720
}
678721
break;
679722
case "li":

0 commit comments

Comments
 (0)