Skip to content

Commit

Permalink
Fix image cache cause copy image not works
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc authored Oct 29, 2024
1 parent 6d2c445 commit e251552
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
14 changes: 10 additions & 4 deletions lib/provider/dio_image_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ class DioImage extends ImageProvider<DioImage> {
/// 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);

/// Creates an object that fetches the image at the given URL.
///
/// 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.
Expand All @@ -56,6 +57,8 @@ class DioImage extends ImageProvider<DioImage> {

final void Function(Uint8List, Headers, String)? onData;

final Key? key;

@override
Future<DioImage> obtainKey(ImageConfiguration configuration) {
return SynchronousFuture<DioImage>(this);
Expand Down Expand Up @@ -164,11 +167,14 @@ class DioImage extends ImageProvider<DioImage> {
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() =>
Expand Down
5 changes: 3 additions & 2 deletions lib/viewer/single.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class SinglePageViewer extends StatefulWidget {

class _SinglePageViewer extends State<SinglePageViewer>
with ThemeModeWidget, IsTopWidget2 {
final Key _key = GlobalKey();
late PageController _pageController;
late int _index;
late GalleryData? _data;
Expand Down Expand Up @@ -143,8 +144,8 @@ class _SinglePageViewer extends State<SinglePageViewer>
_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,
Expand Down

0 comments on commit e251552

Please sign in to comment.