1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| @override ImageStreamCompleter loadBuffer( image_provider.ExtendedNetworkImageProvider key, DecoderBufferCallback decode) { final StreamController<ImageChunkEvent> chunkEvents = StreamController<ImageChunkEvent>();
return MultiFrameImageStreamCompleter( codec: _loadAsync( key as ExtendedNetworkImageProvider, chunkEvents, decode, ), scale: key.scale, chunkEvents: chunkEvents.stream, debugLabel: key.url, informationCollector: () { return <DiagnosticsNode>[ DiagnosticsProperty<ImageProvider>('Image provider', this), DiagnosticsProperty<image_provider.ExtendedNetworkImageProvider>( 'Image key', key), ]; }, ); }
Future<ui.Codec> _loadAsync( ExtendedNetworkImageProvider key, StreamController<ImageChunkEvent> chunkEvents, DecoderBufferCallback decode, ) async { assert(key == this); final String md5Key = cacheKey ?? keyToMd5(key.url); ui.Codec? result; if (cache) { try { final Uint8List? data = await _loadCache( key, chunkEvents, md5Key, ); if (data != null) { result = await instantiateImageCodec(data, decode); } } catch (e) { if (printError) { print(e); } } }
if (result == null) { try { final Uint8List? data = await _loadNetwork( key, chunkEvents, ); if (data != null) { result = await instantiateImageCodec(data, decode); } } catch (e) { if (printError) { print(e); } } }
if (result == null) { return Future<ui.Codec>.error(StateError('Failed to load $url.')); }
return result; }
Future<Uint8List?> _loadCache( ExtendedNetworkImageProvider key, StreamController<ImageChunkEvent>? chunkEvents, String md5Key, ) async { final Directory _cacheImagesDirectory = Directory( join((await getTemporaryDirectory()).path, cacheImageFolderName)); Uint8List? data; if (_cacheImagesDirectory.existsSync()) { final File cacheFlie = File(join(_cacheImagesDirectory.path, md5Key)); if (cacheFlie.existsSync()) { if (key.cacheMaxAge != null) { final DateTime now = DateTime.now(); final FileStat fs = cacheFlie.statSync(); if (now.subtract(key.cacheMaxAge!).isAfter(fs.changed)) { cacheFlie.deleteSync(recursive: true); } else { data = await cacheFlie.readAsBytes(); } } else { data = await cacheFlie.readAsBytes(); } } } else { await _cacheImagesDirectory.create(); } if (data == null) { data = await _loadNetwork( key, chunkEvents, ); if (data != null) { await File(join(_cacheImagesDirectory.path, md5Key)).writeAsBytes(data); } }
return data; }
|