Last active
November 27, 2023 18:18
-
-
Save gabcarneiro/02580611279c85179db015ad2c7ed936 to your computer and use it in GitHub Desktop.
CustomCacheManager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter_cache_manager/flutter_cache_manager.dart'; | |
import 'package:path_provider/path_provider.dart'; | |
import 'package:path/path.dart' as p; | |
class CustomCacheManager extends BaseCacheManager { | |
static const key = 'custom_cache_manager'; | |
static CustomCacheManager _instance; | |
factory CustomCacheManager() { | |
if (_instance == null) { | |
_instance = CustomCacheManager._(); | |
} | |
return _instance; | |
} | |
CustomCacheManager._() | |
: super( | |
key, | |
fileService: CustomHttpFileService(), | |
); | |
Future<String> getFilePath() async { | |
var directory = await getApplicationSupportDirectory(); | |
return p.join(directory.path, key); | |
} | |
} | |
class CustomHttpFileService extends HttpFileService { | |
CustomHttpFileService() : super(); | |
@override | |
Future<FileServiceResponse> get(String url, | |
{Map<String, String> headers = const {}}) { | |
if (url.startsWith('https')) { | |
url = url.replaceFirst('https', 'http'); | |
} | |
return super.get(url, headers: headers); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyComponent extends StatelessWidget { | |
final String url; | |
const MyComponent({Key key, this.url}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return CachedNetworkImage( | |
imageUrl: url, | |
cacheManager: CustomCacheManager(), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment