From 228865446b012970fe6b2c33b6b0363be5489f93 Mon Sep 17 00:00:00 2001 From: acidtar Date: Mon, 13 Dec 2021 16:00:37 +0800 Subject: [PATCH 1/2] feat: null safety support --- lib/flutter_gifimage.dart | 149 ++++++++++++++++++-------------------- 1 file changed, 72 insertions(+), 77 deletions(-) diff --git a/lib/flutter_gifimage.dart b/lib/flutter_gifimage.dart index 4fefcf0..2864336 100644 --- a/lib/flutter_gifimage.dart +++ b/lib/flutter_gifimage.dart @@ -4,26 +4,24 @@ time:2019-7-26 3:30 */ -library flutter_gifimage; - - import 'dart:io'; import 'dart:ui' as ui show Codec; import 'dart:ui'; + import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; /// cache gif fetched image -class GifCache{ - final Map> caches= Map(); +class GifCache { + final Map> caches = Map(); void clear() { caches.clear(); } bool evict(Object key) { - final List pendingImage = caches.remove(key); - if(pendingImage!=null){ + final List? pendingImage = caches.remove(key); + if (pendingImage != null) { return true; } return false; @@ -31,34 +29,31 @@ class GifCache{ } /// controll gif -class GifController extends AnimationController{ - - GifController({ - @required TickerProvider vsync, - double value=0.0, - Duration reverseDuration, - Duration duration, - AnimationBehavior animationBehavior - }):super.unbounded( - value:value, - reverseDuration:reverseDuration, - duration:duration, - animationBehavior:animationBehavior??AnimationBehavior.normal, - vsync:vsync); +class GifController extends AnimationController { + GifController( + {required TickerProvider vsync, + double value = 0.0, + Duration? reverseDuration, + Duration? duration, + AnimationBehavior? animationBehavior}) + : super.unbounded( + value: value, + reverseDuration: reverseDuration, + duration: duration, + animationBehavior: animationBehavior ?? AnimationBehavior.normal, + vsync: vsync); @override void reset() { // TODO: implement reset value = 0.0; } - } - -class GifImage extends StatefulWidget{ +class GifImage extends StatefulWidget { GifImage({ - @required this.image, - @required this.controller, + required this.image, + required this.controller, this.semanticLabel, this.excludeFromSemantics = false, this.width, @@ -73,20 +68,21 @@ class GifImage extends StatefulWidget{ this.matchTextDirection = false, this.gaplessPlayback = false, }); - final VoidCallback onFetchCompleted; + + final VoidCallback? onFetchCompleted; final GifController controller; final ImageProvider image; - final double width; - final double height; - final Color color; - final BlendMode colorBlendMode; - final BoxFit fit; + final double? width; + final double? height; + final Color? color; + final BlendMode? colorBlendMode; + final BoxFit? fit; final AlignmentGeometry alignment; final ImageRepeat repeat; - final Rect centerSlice; + final Rect? centerSlice; final bool matchTextDirection; final bool gaplessPlayback; - final String semanticLabel; + final String? semanticLabel; final bool excludeFromSemantics; @override @@ -97,16 +93,15 @@ class GifImage extends StatefulWidget{ static GifCache cache = GifCache(); } -class GifImageState extends State{ - List _infos; +class GifImageState extends State { + List? _infos; int _curIndex = 0; - bool _fetchComplete= false; - ImageInfo get _imageInfo { - if(!_fetchComplete)return null; - return _infos==null?null:_infos[_curIndex]; - } - + bool _fetchComplete = false; + ImageInfo? get _imageInfo { + if (!_fetchComplete) return null; + return _infos == null ? null : _infos![_curIndex]; + } @override void initState() { @@ -124,14 +119,14 @@ class GifImageState extends State{ void didUpdateWidget(GifImage oldWidget) { super.didUpdateWidget(oldWidget); if (widget.image != oldWidget.image) { - fetchGif(widget.image).then((imageInfors){ - if(mounted) + fetchGif(widget.image).then((imageInfors) { + if (mounted) setState(() { _infos = imageInfors; - _fetchComplete=true; + _fetchComplete = true; _curIndex = widget.controller.value.toInt(); - if(widget.onFetchCompleted!=null){ - widget.onFetchCompleted(); + if (widget.onFetchCompleted != null) { + widget.onFetchCompleted!(); } }); }); @@ -142,9 +137,9 @@ class GifImageState extends State{ } } - void _listener(){ - if(_curIndex!=widget.controller.value&&_fetchComplete){ - if(mounted) + void _listener() { + if (_curIndex != widget.controller.value && _fetchComplete) { + if (mounted) setState(() { _curIndex = widget.controller.value.toInt(); }); @@ -154,15 +149,15 @@ class GifImageState extends State{ @override void didChangeDependencies() { super.didChangeDependencies(); - if(_infos==null){ - fetchGif(widget.image).then((imageInfors){ - if(mounted) + if (_infos == null) { + fetchGif(widget.image).then((imageInfors) { + if (mounted) setState(() { _infos = imageInfors; - _fetchComplete=true; + _fetchComplete = true; _curIndex = widget.controller.value.toInt(); - if(widget.onFetchCompleted!=null){ - widget.onFetchCompleted(); + if (widget.onFetchCompleted != null) { + widget.onFetchCompleted!(); } }); }); @@ -184,8 +179,7 @@ class GifImageState extends State{ centerSlice: widget.centerSlice, matchTextDirection: widget.matchTextDirection, ); - if (widget.excludeFromSemantics) - return image; + if (widget.excludeFromSemantics) return image; return new Semantics( container: widget.semanticLabel != null, image: true, @@ -195,30 +189,33 @@ class GifImageState extends State{ } } - - final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false; HttpClient get _httpClient { HttpClient client = _sharedHttpClient; assert(() { if (debugNetworkImageHttpClientProvider != null) - client = debugNetworkImageHttpClientProvider(); + client = debugNetworkImageHttpClientProvider!(); return true; }()); return client; } - -Future> fetchGif(ImageProvider provider) async{ +Future> fetchGif(ImageProvider provider) async { List infos = []; 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]; + 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 infos; } - if(provider is NetworkImage){ + if (provider is NetworkImage) { final Uri resolved = Uri.base.resolve(provider.url); final HttpClientRequest request = await _httpClient.getUrl(resolved); provider.headers?.forEach((String name, String value) { @@ -228,25 +225,23 @@ Future> fetchGif(ImageProvider provider) async{ data = await consolidateHttpClientResponseBytes( response, ); - } - else if(provider is AssetImage){ + } else if (provider is AssetImage) { AssetBundleImageKey key = await provider.obtainKey(ImageConfiguration()); data = await key.bundle.load(key.name); - } - else if(provider is FileImage){ + } else if (provider is FileImage) { data = await provider.file.readAsBytes(); - } - else if(provider is MemoryImage){ - data = provider.bytes; + } else if (provider is MemoryImage) { + data = provider.bytes; } - ui.Codec codec=await PaintingBinding.instance.instantiateImageCodec(data.buffer.asUint8List()); + ui.Codec codec = await PaintingBinding.instance + !.instantiateImageCodec(data.buffer.asUint8List()); infos = []; - for(int i = 0;i infos); return infos; -} \ No newline at end of file +} From 0785cc37a846447e4f7679f5b3fc0d7d25d8650e Mon Sep 17 00:00:00 2001 From: acidtar Date: Mon, 13 Dec 2021 16:02:13 +0800 Subject: [PATCH 2/2] chore: add "library flutter_gifimage" --- lib/flutter_gifimage.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/flutter_gifimage.dart b/lib/flutter_gifimage.dart index 2864336..89b0377 100644 --- a/lib/flutter_gifimage.dart +++ b/lib/flutter_gifimage.dart @@ -4,10 +4,13 @@ time:2019-7-26 3:30 */ +library flutter_gifimage; + + + import 'dart:io'; import 'dart:ui' as ui show Codec; import 'dart:ui'; - import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart';