Skip to content
This repository was archived by the owner on Dec 26, 2024. It is now read-only.

Commit 4d0449c

Browse files
Merge pull request #13 from DawenZhang/master
extract imagespan out of nested children to be displayable
2 parents 5ad3eac + cce0635 commit 4d0449c

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

lib/real_rich_text.dart

+34-4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ class RealRichText extends Text {
6363
maxLines: maxLines,
6464
locale: locale);
6565

66+
List<TextSpan> extractAllNestedChildren(TextSpan textSpan) {
67+
if (textSpan.children == null || textSpan.children.length == 0) {
68+
return [textSpan];
69+
}
70+
List<TextSpan> childrenSpan = [];
71+
textSpan.children.forEach((child) {
72+
childrenSpan.addAll(extractAllNestedChildren(child));
73+
});
74+
return childrenSpan;
75+
}
76+
6677
@override
6778
Widget build(BuildContext context) {
6879
final DefaultTextStyle defaultTextStyle = DefaultTextStyle.of(context);
@@ -74,10 +85,13 @@ class RealRichText extends Text {
7485
.merge(const TextStyle(fontWeight: FontWeight.bold));
7586

7687
TextSpan textSpan = TextSpan(
77-
style: effectiveTextStyle,
78-
text: "",
79-
children: textSpanList,
80-
);
88+
style: effectiveTextStyle,
89+
text: "",
90+
children: extractAllNestedChildren(TextSpan(
91+
style: effectiveTextStyle,
92+
text: "",
93+
children: textSpanList,
94+
)));
8195

8296
// pass the context to ImageSpan to create a ImageConfiguration
8397
textSpan.children.forEach((f) {
@@ -189,6 +203,12 @@ class ImageResolver {
189203
_listener?.call(imageInfo, synchronousCall);
190204
}
191205

206+
void addListening() {
207+
if (this._listener != null) {
208+
_imageStream?.addListener(_handleImageChanged);
209+
}
210+
}
211+
192212
void stopListening() {
193213
_imageStream?.removeListener(_handleImageChanged);
194214
}
@@ -271,6 +291,16 @@ class _RealRichRenderParagraph extends RenderParagraph {
271291
paintImageSpan(context, offset);
272292
}
273293

294+
@override
295+
void attach(covariant Object owner) {
296+
super.attach(owner);
297+
text.children.forEach((textSpan) {
298+
if (textSpan is ImageSpan) {
299+
textSpan.imageResolver.addListening();
300+
}
301+
});
302+
}
303+
274304
@override
275305
void detach() {
276306
super.detach();

0 commit comments

Comments
 (0)