add gifcache

master
Jpeng 5 years ago
parent 03aa48385d
commit 3abbcaae71

@ -13,7 +13,23 @@ import 'dart:ui';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
class GifCache{
final Map<String,List<ImageInfo>> caches= Map();
void clear() {
caches.clear();
}
bool evict(Object key) {
final List<ImageInfo> pendingImage = caches.remove(key);
if(pendingImage!=null){
return true;
}
return false;
}
}
/// controll gif /// controll gif
class GifController extends AnimationController{ class GifController extends AnimationController{
@ -70,6 +86,8 @@ class GifImage extends StatefulWidget{
State<StatefulWidget> createState() { State<StatefulWidget> createState() {
return new GifImageState(); return new GifImageState();
} }
static GifCache cache = GifCache();
} }
class GifImageState extends State<GifImage>{ class GifImageState extends State<GifImage>{
@ -93,18 +111,21 @@ class GifImageState extends State<GifImage>{
return client; return client;
} }
void _fetchImage() async{ Future<void> _fetchImage() async{
ImageProvider provider = widget.image; ImageProvider provider = widget.image;
dynamic data; dynamic data;
String key =provider is NetworkImage?provider.url:provider is AssetImage?provider.assetName:provider is MemoryImage?provider.bytes.toString():"";
if(GifImage.cache.caches.containsKey(key)){
_infos = GifImage.cache.caches[key];
return ;
}
if(provider is NetworkImage){ if(provider is NetworkImage){
final Uri resolved = Uri.base.resolve(provider.url); final Uri resolved = Uri.base.resolve(provider.url);
final HttpClientRequest request = await _httpClient.getUrl(resolved); final HttpClientRequest request = await _httpClient.getUrl(resolved);
provider.headers?.forEach((String name, String value) { provider.headers?.forEach((String name, String value) {
request.headers.add(name, value); request.headers.add(name, value);
}); });
final HttpClientResponse response = await request.close(); final HttpClientResponse response = await request.close();
data = await consolidateHttpClientResponseBytes( data = await consolidateHttpClientResponseBytes(
response, response,
); );
@ -128,14 +149,8 @@ class GifImageState extends State<GifImage>{
//scale ?? //scale ??
_infos.add(ImageInfo(image: frameInfo.image)); _infos.add(ImageInfo(image: frameInfo.image));
} }
GifImage.cache.caches.putIfAbsent(key, () => _infos);
setState(() {
_fetchComplete=true;
_curIndex = widget.controller.value.toInt();
if(widget.onFetchCompleted!=null){
widget.onFetchCompleted();
}
});
} }
@override @override
@ -154,7 +169,15 @@ class GifImageState extends State<GifImage>{
void didUpdateWidget(GifImage oldWidget) { void didUpdateWidget(GifImage oldWidget) {
super.didUpdateWidget(oldWidget); super.didUpdateWidget(oldWidget);
if (widget.image != oldWidget.image) { if (widget.image != oldWidget.image) {
_fetchImage(); _fetchImage().then((_){
setState(() {
_fetchComplete=true;
_curIndex = widget.controller.value.toInt();
if(widget.onFetchCompleted!=null){
widget.onFetchCompleted();
}
});
});
} }
if (widget.controller != oldWidget.controller) { if (widget.controller != oldWidget.controller) {
oldWidget.controller.removeListener(_listener); oldWidget.controller.removeListener(_listener);
@ -174,7 +197,15 @@ class GifImageState extends State<GifImage>{
void didChangeDependencies() { void didChangeDependencies() {
super.didChangeDependencies(); super.didChangeDependencies();
if(_infos==null){ if(_infos==null){
_fetchImage(); _fetchImage().then((_){
setState(() {
_fetchComplete=true;
_curIndex = widget.controller.value.toInt();
if(widget.onFetchCompleted!=null){
widget.onFetchCompleted();
}
});
});
} }
} }

Loading…
Cancel
Save