Skip to content

Commit a8abf50

Browse files
fix the render will no stop when render gif using flutter html bug[780]
1 parent e713e3e commit a8abf50

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

lib/image_render.dart

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ typedef ImageSourceMatcher = bool Function(
1212
dom.Element? element,
1313
);
1414

15-
final _dataUriFormat = RegExp("^(?<scheme>data):(?<mime>image\/[\\w\+\-\.]+)(?<encoding>;base64)?\,(?<data>.*)");
15+
final _dataUriFormat = RegExp(
16+
"^(?<scheme>data):(?<mime>image\/[\\w\+\-\.]+)(?<encoding>;base64)?\,(?<data>.*)");
1617

17-
ImageSourceMatcher dataUriMatcher({String? encoding = 'base64', String? mime}) => (attributes, element) {
18+
ImageSourceMatcher dataUriMatcher(
19+
{String? encoding = 'base64', String? mime}) =>
20+
(attributes, element) {
1821
if (_src(attributes) == null) return false;
1922
final dataUri = _dataUriFormat.firstMatch(_src(attributes)!);
2023
return dataUri != null &&
@@ -62,7 +65,8 @@ ImageRender base64ImageRender() => (context, attributes, element) {
6265
decodedImage,
6366
frameBuilder: (ctx, child, frame, _) {
6467
if (frame == null) {
65-
return Text(_alt(attributes) ?? "", style: context.style.generateTextStyle());
68+
return Text(_alt(attributes) ?? "",
69+
style: context.style.generateTextStyle());
6670
}
6771
return child;
6872
},
@@ -76,18 +80,18 @@ ImageRender assetImageRender({
7680
(context, attributes, element) {
7781
final assetPath = _src(attributes)!.replaceFirst('asset:', '');
7882
if (_src(attributes)!.endsWith(".svg")) {
79-
return SvgPicture.asset(
80-
assetPath,
81-
width: width ?? _width(attributes),
82-
height: height ?? _height(attributes));
83+
return SvgPicture.asset(assetPath,
84+
width: width ?? _width(attributes),
85+
height: height ?? _height(attributes));
8386
} else {
8487
return Image.asset(
8588
assetPath,
8689
width: width ?? _width(attributes),
8790
height: height ?? _height(attributes),
8891
frameBuilder: (ctx, child, frame, _) {
8992
if (frame == null) {
90-
return Text(_alt(attributes) ?? "", style: context.style.generateTextStyle());
93+
return Text(_alt(attributes) ?? "",
94+
style: context.style.generateTextStyle());
9195
}
9296
return child;
9397
},
@@ -127,28 +131,33 @@ ImageRender networkImageRender({
127131
}
128132
});
129133

130-
image.image.resolve(ImageConfiguration()).addListener(
131-
ImageStreamListener((ImageInfo image, bool synchronousCall) {
132-
var myImage = image.image;
133-
Size size = Size(myImage.width.toDouble(), myImage.height.toDouble());
134-
if (!completer.isCompleted) {
135-
completer.complete(size);
136-
}
137-
}, onError: (object, stacktrace) {
138-
if (!completer.isCompleted) {
139-
completer.completeError(object);
140-
}
141-
}),
142-
);
134+
var listener =
135+
ImageStreamListener((ImageInfo image, bool synchronousCall) {
136+
var myImage = image.image;
137+
Size size = Size(myImage.width.toDouble(), myImage.height.toDouble());
138+
if (!completer.isCompleted) {
139+
completer.complete(size);
140+
}
141+
}, onError: (object, stacktrace) {
142+
if (!completer.isCompleted) {
143+
completer.completeError(object);
144+
}
145+
});
146+
147+
image.image.resolve(ImageConfiguration()).addListener(listener);
143148
return FutureBuilder<Size>(
144149
future: completer.future,
145150
builder: (BuildContext buildContext, AsyncSnapshot<Size> snapshot) {
151+
if (completer.isCompleted) {
152+
image.image.resolve(ImageConfiguration()).removeListener(listener);
153+
}
146154
if (snapshot.hasData) {
147155
return Container(
148156
constraints: BoxConstraints(
149-
maxWidth: width ?? _width(attributes) ?? snapshot.data!.width,
150-
maxHeight: (width ?? _width(attributes) ?? snapshot.data!.width) / _aspectRatio(attributes, snapshot)
151-
),
157+
maxWidth: width ?? _width(attributes) ?? snapshot.data!.width,
158+
maxHeight:
159+
(width ?? _width(attributes) ?? snapshot.data!.width) /
160+
_aspectRatio(attributes, snapshot)),
152161
child: AspectRatio(
153162
aspectRatio: _aspectRatio(attributes, snapshot),
154163
child: Image.network(
@@ -159,7 +168,8 @@ ImageRender networkImageRender({
159168
frameBuilder: (ctx, child, frame, _) {
160169
if (frame == null) {
161170
return altWidget?.call(_alt(attributes)) ??
162-
Text(_alt(attributes) ?? "", style: context.style.generateTextStyle());
171+
Text(_alt(attributes) ?? "",
172+
style: context.style.generateTextStyle());
163173
}
164174
return child;
165175
},
@@ -168,7 +178,8 @@ ImageRender networkImageRender({
168178
);
169179
} else if (snapshot.hasError) {
170180
return altWidget?.call(_alt(attributes)) ??
171-
Text(_alt(attributes) ?? "", style: context.style.generateTextStyle());
181+
Text(_alt(attributes) ?? "",
182+
style: context.style.generateTextStyle());
172183
} else {
173184
return loadingWidget?.call() ?? const CircularProgressIndicator();
174185
}
@@ -217,21 +228,28 @@ String? _alt(Map<String, String> attributes) {
217228

218229
double? _height(Map<String, String> attributes) {
219230
final heightString = attributes["height"];
220-
return heightString == null ? heightString as double? : double.tryParse(heightString);
231+
return heightString == null
232+
? heightString as double?
233+
: double.tryParse(heightString);
221234
}
222235

223236
double? _width(Map<String, String> attributes) {
224237
final widthString = attributes["width"];
225-
return widthString == null ? widthString as double? : double.tryParse(widthString);
238+
return widthString == null
239+
? widthString as double?
240+
: double.tryParse(widthString);
226241
}
227242

228-
double _aspectRatio(Map<String, String> attributes, AsyncSnapshot<Size> calculated) {
243+
double _aspectRatio(
244+
Map<String, String> attributes, AsyncSnapshot<Size> calculated) {
229245
final heightString = attributes["height"];
230246
final widthString = attributes["width"];
231247
if (heightString != null && widthString != null) {
232248
final height = double.tryParse(heightString);
233249
final width = double.tryParse(widthString);
234-
return height == null || width == null ? calculated.data!.aspectRatio : width / height;
250+
return height == null || width == null
251+
? calculated.data!.aspectRatio
252+
: width / height;
235253
}
236254
return calculated.data!.aspectRatio;
237255
}

0 commit comments

Comments
 (0)