@@ -12,9 +12,12 @@ typedef ImageSourceMatcher = bool Function(
12
12
dom.Element ? element,
13
13
);
14
14
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>.*)" );
16
17
17
- ImageSourceMatcher dataUriMatcher ({String ? encoding = 'base64' , String ? mime}) => (attributes, element) {
18
+ ImageSourceMatcher dataUriMatcher (
19
+ {String ? encoding = 'base64' , String ? mime}) =>
20
+ (attributes, element) {
18
21
if (_src (attributes) == null ) return false ;
19
22
final dataUri = _dataUriFormat.firstMatch (_src (attributes)! );
20
23
return dataUri != null &&
@@ -62,7 +65,8 @@ ImageRender base64ImageRender() => (context, attributes, element) {
62
65
decodedImage,
63
66
frameBuilder: (ctx, child, frame, _) {
64
67
if (frame == null ) {
65
- return Text (_alt (attributes) ?? "" , style: context.style.generateTextStyle ());
68
+ return Text (_alt (attributes) ?? "" ,
69
+ style: context.style.generateTextStyle ());
66
70
}
67
71
return child;
68
72
},
@@ -76,18 +80,18 @@ ImageRender assetImageRender({
76
80
(context, attributes, element) {
77
81
final assetPath = _src (attributes)! .replaceFirst ('asset:' , '' );
78
82
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));
83
86
} else {
84
87
return Image .asset (
85
88
assetPath,
86
89
width: width ?? _width (attributes),
87
90
height: height ?? _height (attributes),
88
91
frameBuilder: (ctx, child, frame, _) {
89
92
if (frame == null ) {
90
- return Text (_alt (attributes) ?? "" , style: context.style.generateTextStyle ());
93
+ return Text (_alt (attributes) ?? "" ,
94
+ style: context.style.generateTextStyle ());
91
95
}
92
96
return child;
93
97
},
@@ -127,28 +131,33 @@ ImageRender networkImageRender({
127
131
}
128
132
});
129
133
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);
143
148
return FutureBuilder <Size >(
144
149
future: completer.future,
145
150
builder: (BuildContext buildContext, AsyncSnapshot <Size > snapshot) {
151
+ if (completer.isCompleted) {
152
+ image.image.resolve (ImageConfiguration ()).removeListener (listener);
153
+ }
146
154
if (snapshot.hasData) {
147
155
return Container (
148
156
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)),
152
161
child: AspectRatio (
153
162
aspectRatio: _aspectRatio (attributes, snapshot),
154
163
child: Image .network (
@@ -159,7 +168,8 @@ ImageRender networkImageRender({
159
168
frameBuilder: (ctx, child, frame, _) {
160
169
if (frame == null ) {
161
170
return altWidget? .call (_alt (attributes)) ??
162
- Text (_alt (attributes) ?? "" , style: context.style.generateTextStyle ());
171
+ Text (_alt (attributes) ?? "" ,
172
+ style: context.style.generateTextStyle ());
163
173
}
164
174
return child;
165
175
},
@@ -168,7 +178,8 @@ ImageRender networkImageRender({
168
178
);
169
179
} else if (snapshot.hasError) {
170
180
return altWidget? .call (_alt (attributes)) ??
171
- Text (_alt (attributes) ?? "" , style: context.style.generateTextStyle ());
181
+ Text (_alt (attributes) ?? "" ,
182
+ style: context.style.generateTextStyle ());
172
183
} else {
173
184
return loadingWidget? .call () ?? const CircularProgressIndicator ();
174
185
}
@@ -217,21 +228,28 @@ String? _alt(Map<String, String> attributes) {
217
228
218
229
double ? _height (Map <String , String > attributes) {
219
230
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);
221
234
}
222
235
223
236
double ? _width (Map <String , String > attributes) {
224
237
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);
226
241
}
227
242
228
- double _aspectRatio (Map <String , String > attributes, AsyncSnapshot <Size > calculated) {
243
+ double _aspectRatio (
244
+ Map <String , String > attributes, AsyncSnapshot <Size > calculated) {
229
245
final heightString = attributes["height" ];
230
246
final widthString = attributes["width" ];
231
247
if (heightString != null && widthString != null ) {
232
248
final height = double .tryParse (heightString);
233
249
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;
235
253
}
236
254
return calculated.data! .aspectRatio;
237
255
}
0 commit comments