From e2515520d6692f31572d064aaf17346086bd8ae3 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 29 Oct 2024 11:23:30 +0000 Subject: [PATCH] Fix image cache cause copy image not works --- lib/provider/dio_image_provider.dart | 14 ++++++++++---- lib/viewer/single.dart | 5 +++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/provider/dio_image_provider.dart b/lib/provider/dio_image_provider.dart index cc10f61..5e50fc6 100644 --- a/lib/provider/dio_image_provider.dart +++ b/lib/provider/dio_image_provider.dart @@ -29,7 +29,7 @@ class DioImage extends ImageProvider { /// The arguments [url] and [scale] must not be null. /// [dio] will be the default [Dio] if not set. DioImage.string(String url, - {this.scale = 1.0, this.headers, this.onData, Dio? dio}) + {this.scale = 1.0, this.headers, this.onData, Dio? dio, this.key}) : dio = dio ?? defaultDio, url = Uri.parse(url); @@ -37,7 +37,8 @@ class DioImage extends ImageProvider { /// /// The arguments [url] and [scale] must not be null. /// [dio] will be the default [Dio] if not set. - DioImage(this.url, {this.scale = 1.0, this.headers, this.onData, Dio? dio}) + DioImage(this.url, + {this.scale = 1.0, this.headers, this.onData, this.key, Dio? dio}) : dio = dio ?? defaultDio; /// The URL from which the image will be fetched. @@ -56,6 +57,8 @@ class DioImage extends ImageProvider { final void Function(Uint8List, Headers, String)? onData; + final Key? key; + @override Future obtainKey(ImageConfiguration configuration) { return SynchronousFuture(this); @@ -164,11 +167,14 @@ class DioImage extends ImageProvider { if (other.runtimeType != runtimeType) { return false; } - return other is DioImage && other.url == url && other.scale == scale; + return other is DioImage && + other.url == url && + other.scale == scale && + other.key == key; } @override - int get hashCode => Object.hash(url, scale); + int get hashCode => Object.hash(url, scale, key); @override String toString() => diff --git a/lib/viewer/single.dart b/lib/viewer/single.dart index 82820ac..84e8a79 100644 --- a/lib/viewer/single.dart +++ b/lib/viewer/single.dart @@ -47,6 +47,7 @@ class SinglePageViewer extends StatefulWidget { class _SinglePageViewer extends State with ThemeModeWidget, IsTopWidget2 { + final Key _key = GlobalKey(); late PageController _pageController; late int _index; late GalleryData? _data; @@ -143,8 +144,8 @@ class _SinglePageViewer extends State _photoViewController.reset(); } return PhotoViewGalleryPageOptions( - imageProvider: DioImage.string(api.getFileUrl(f.id), dio: dio, - onData: (data, headers, url) { + imageProvider: DioImage.string(api.getFileUrl(f.id), + dio: dio, key: _key, onData: (data, headers, url) { _imgData[index] = (data, headers.value("content-type"), url); }), initialScale: PhotoViewComputedScale.contained,