@@ -3,12 +3,13 @@ import 'dart:io';
33
44import 'package:cached_network_image/cached_network_image.dart' ;
55import 'package:flutter/material.dart' ;
6- import 'package:piwigo_ng/api /api_client.dart' ;
6+ import 'package:piwigo_ng/network /api_client.dart' ;
77import 'package:piwigo_ng/services/preferences_service.dart' ;
8+ import 'package:piwigo_ng/utils/settings.dart' ;
89import 'package:shared_preferences/shared_preferences.dart' ;
910
10- class AppImageDisplay extends StatefulWidget {
11- const AppImageDisplay ({
11+ class ImageNetworkDisplay extends StatefulWidget {
12+ const ImageNetworkDisplay ({
1213 Key ? key,
1314 this .imageUrl,
1415 this .fit,
@@ -18,10 +19,10 @@ class AppImageDisplay extends StatefulWidget {
1819 final BoxFit ? fit;
1920
2021 @override
21- State <AppImageDisplay > createState () => _AppImageDisplayState ();
22+ State <ImageNetworkDisplay > createState () => _ImageNetworkDisplayState ();
2223}
2324
24- class _AppImageDisplayState extends State <AppImageDisplay > {
25+ class _ImageNetworkDisplayState extends State <ImageNetworkDisplay > {
2526 late final Future <Map <String , String >> _headers;
2627
2728 @override
@@ -57,32 +58,49 @@ class _AppImageDisplayState extends State<AppImageDisplay> {
5758 };
5859 }
5960
61+ void _checkMemory () {
62+ ImageCache imageCache = PaintingBinding .instance.imageCache;
63+ if (imageCache.liveImageCount >= Settings .maxCacheLiveImages) {
64+ imageCache.clear ();
65+ imageCache.clearLiveImages ();
66+ }
67+ }
68+
6069 @override
6170 Widget build (BuildContext context) {
6271 if (widget.imageUrl == null ) {
6372 return _buildNoImageWidget (context);
6473 }
65-
74+ _checkMemory ();
6675 return FutureBuilder <Map <String , String >>(
6776 future: _headers,
6877 builder: (context, snapshot) {
6978 if (snapshot.hasData) {
70- return CachedNetworkImage (
71- imageUrl: widget.imageUrl! ,
72- fadeInDuration: const Duration (milliseconds: 300 ),
73- fit: widget.fit ?? BoxFit .cover,
74- httpHeaders: snapshot.data! ,
75- imageBuilder: (context, provider) => Image (
76- image: provider,
79+ return LayoutBuilder (builder: (context, constraints) {
80+ double ? cacheWidth =
81+ constraints.maxWidth.isInfinite ? constraints.maxWidth : null ;
82+ double ? cacheHeight = constraints.maxHeight.isInfinite
83+ ? constraints.maxHeight
84+ : null ;
85+ return CachedNetworkImage (
86+ imageUrl: widget.imageUrl! ,
87+ fadeInDuration: const Duration (milliseconds: 300 ),
7788 fit: widget.fit ?? BoxFit .cover,
78- errorBuilder: (context, o, s) {
79- debugPrint ("$o \n $s " );
80- return _buildErrorWidget (context, widget.imageUrl, o);
81- },
82- ),
83- progressIndicatorBuilder: _buildProgressIndicator,
84- errorWidget: _buildErrorWidget,
85- );
89+ httpHeaders: snapshot.data! ,
90+ memCacheWidth: cacheWidth? .floor (),
91+ memCacheHeight: cacheHeight? .floor (),
92+ imageBuilder: (context, provider) => Image (
93+ image: provider,
94+ fit: widget.fit ?? BoxFit .cover,
95+ errorBuilder: (context, o, s) {
96+ debugPrint ("$o \n $s " );
97+ return _buildErrorWidget (context, widget.imageUrl, o);
98+ },
99+ ),
100+ progressIndicatorBuilder: _buildProgressIndicator,
101+ errorWidget: _buildErrorWidget,
102+ );
103+ });
86104 }
87105 if (snapshot.hasError) {
88106 return _buildErrorWidget (context);
@@ -94,7 +112,10 @@ class _AppImageDisplayState extends State<AppImageDisplay> {
94112 }
95113
96114 Widget _buildProgressIndicator (
97- BuildContext context, String url, DownloadProgress download) {
115+ BuildContext context,
116+ String url,
117+ DownloadProgress download,
118+ ) {
98119 if (download.downloaded >= (download.totalSize ?? 0 )) {
99120 return const SizedBox ();
100121 }
@@ -105,7 +126,11 @@ class _AppImageDisplayState extends State<AppImageDisplay> {
105126 );
106127 }
107128
108- Widget _buildErrorWidget (BuildContext context, [String ? url, dynamic error]) {
129+ Widget _buildErrorWidget (
130+ BuildContext context, [
131+ String ? url,
132+ dynamic error,
133+ ]) {
109134 debugPrint ("[$url !] $error " );
110135 return FittedBox (
111136 fit: BoxFit .cover,
0 commit comments