Skip to content

Commit f244ce9

Browse files
committed
Fix bug and tests
1 parent 6c4835a commit f244ce9

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

flutter_cache_manager/lib/src/cache_managers/image_cache_manager.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,16 @@ mixin ImageCacheManager on BaseCacheManager {
133133
}
134134
}
135135

136-
Future<ui.Image> _decodeImage(File file, {int? width, int? height, bool allowUpscaling = false}) {
137-
// since we set allowUpscaling to false, the width and height parameters are actually max-width.
138-
final image = ResizeImage(FileImage(file), width: width, height: height, allowUpscaling: allowUpscaling);
136+
Future<ui.Image> _decodeImage(File file,
137+
{int? width, int? height, bool allowUpscaling = false}) {
138+
var shouldResize = width != null || height != null;
139+
var fileImage = FileImage(file);
140+
final image = shouldResize
141+
? ResizeImage(fileImage,
142+
width: width, height: height, allowUpscaling: allowUpscaling)
143+
: fileImage as ImageProvider;
139144
final completer = Completer<ui.Image>();
140-
image
141-
.resolve(const ImageConfiguration())
142-
.addListener(ImageStreamListener((info, _) => completer.complete(info.image)));
145+
image.resolve(const ImageConfiguration()).addListener(
146+
ImageStreamListener((info, _) => completer.complete(info.image)));
143147
return completer.future;
144148
}

flutter_cache_manager/test/image_cache_manager_test.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:io' as io;
22
import 'dart:typed_data';
33
import 'dart:ui';
44

5+
import 'package:flutter/material.dart';
56
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
67
import 'package:flutter_test/flutter_test.dart';
78
import 'cache_manager_test.dart';
@@ -13,6 +14,17 @@ const fileName = 'test.jpg';
1314
const fileUrl = 'baseflow.com/test';
1415
final validTill = DateTime.now().add(const Duration(days: 1));
1516
void main() {
17+
18+
setUp(() {
19+
WidgetsFlutterBinding.ensureInitialized();
20+
});
21+
22+
tearDown(() {
23+
PaintingBinding.instance?.imageCache?.clear();
24+
PaintingBinding.instance?.imageCache?.clearLiveImages();
25+
});
26+
27+
1628
group('Test image resizing', () {
1729
test('Test original image size', () async {
1830
final bytes = await getExampleImage();

0 commit comments

Comments
 (0)