diff --git a/CHANGELOG.md b/CHANGELOG.md index ac07159..b639ede 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ ## [0.0.1] - TODO: Add release date. * TODO: Describe initial release. + +## 1.0.0 +* init release diff --git a/LICENSE b/LICENSE index ba75c69..2c25915 100644 --- a/LICENSE +++ b/LICENSE @@ -1 +1,21 @@ -TODO: Add your license here. +MIT License + +Copyright (c) 2019 Jpeng + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 98a5af1..ace0208 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,49 @@ # flutter_gifimage -A new Flutter package project. +We should know that in order to achieve Gif in flutter, we can use Image, but we have no way to manipulate Gif, for example: change its speed, control it has been playing in a frame, + in which frame range loop. These problems can be solved by this control. -## Getting Started +* Usage(Simple) -This project is a starting point for a Dart -[package](https://flutter.dev/developing-packages/), -a library module containing code that can be shared easily across -multiple Flutter or Dart projects. + ```dart + controller4= GifController(vsync: this); -For help getting started with Flutter, view our -[online documentation](https://flutter.dev/docs), which offers tutorials, -samples, guidance on mobile development, and a full API reference. + + GifImage( + controller: controller, + image: MemoryImage(base64Decode(base64_url)), + ) + + ``` + GifController is just a AnimationController, usage is the same with AnimationController,If you want to play animate Between two frames ,you just called 'repeat(min: max:)','period' can change the repeat speed + ,If you want to stop in one frame , call 'controller.value = .. ' + +# Thanks +* [gif_ani](https://github.com/hyz1992/gif_ani) (thanks for giving me idea) + +# License + +``` +MIT License + +Copyright (c) 2019 Jpeng + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +``` \ No newline at end of file diff --git a/lib/flutter_gifimage.dart b/lib/flutter_gifimage.dart index dd04bd1..78b8fd4 100644 --- a/lib/flutter_gifimage.dart +++ b/lib/flutter_gifimage.dart @@ -99,59 +99,7 @@ class GifImageState extends State{ return _infos==null?null:_infos[_curIndex]; } - static final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false; - static HttpClient get _httpClient { - HttpClient client = _sharedHttpClient; - assert(() { - if (debugNetworkImageHttpClientProvider != null) - client = debugNetworkImageHttpClientProvider(); - return true; - }()); - return client; - } - - Future _fetchImage() async{ - ImageProvider provider = widget.image; - 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){ - final Uri resolved = Uri.base.resolve(provider.url); - final HttpClientRequest request = await _httpClient.getUrl(resolved); - provider.headers?.forEach((String name, String value) { - request.headers.add(name, value); - }); - final HttpClientResponse response = await request.close(); - data = await consolidateHttpClientResponseBytes( - response, - ); - - } - else if(provider is AssetImage){ - AssetBundleImageKey key = await provider.obtainKey(ImageConfiguration()); - data = await key.bundle.load(key.name); - } - else if(provider is FileImage){ - data = await provider.file.readAsBytes(); - } - else if(provider is MemoryImage){ - data = provider.bytes; - } - - ui.Codec codec=await PaintingBinding.instance.instantiateImageCodec(data.buffer.asUint8List()); - _infos = []; - for(int i = 0;i _infos); - - } @override void initState() { @@ -169,8 +117,9 @@ class GifImageState extends State{ void didUpdateWidget(GifImage oldWidget) { super.didUpdateWidget(oldWidget); if (widget.image != oldWidget.image) { - _fetchImage().then((_){ + fetchGif(widget.image).then((imageInfors){ setState(() { + _infos = imageInfors; _fetchComplete=true; _curIndex = widget.controller.value.toInt(); if(widget.onFetchCompleted!=null){ @@ -197,8 +146,9 @@ class GifImageState extends State{ void didChangeDependencies() { super.didChangeDependencies(); if(_infos==null){ - _fetchImage().then((_){ + fetchGif(widget.image).then((imageInfors){ setState(() { + _infos = imageInfors; _fetchComplete=true; _curIndex = widget.controller.value.toInt(); if(widget.onFetchCompleted!=null){ @@ -234,3 +184,59 @@ class GifImageState extends State{ ); } } + + + +final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false; + +HttpClient get _httpClient { + HttpClient client = _sharedHttpClient; + assert(() { + if (debugNetworkImageHttpClientProvider != null) + client = debugNetworkImageHttpClientProvider(); + return true; + }()); + return client; +} + + +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]; + return infos; + } + 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) { + request.headers.add(name, value); + }); + final HttpClientResponse response = await request.close(); + data = await consolidateHttpClientResponseBytes( + response, + ); + } + else if(provider is AssetImage){ + AssetBundleImageKey key = await provider.obtainKey(ImageConfiguration()); + data = await key.bundle.load(key.name); + } + else if(provider is FileImage){ + data = await provider.file.readAsBytes(); + } + else if(provider is MemoryImage){ + data = provider.bytes; + } + + 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 diff --git a/pubspec.lock b/pubspec.lock index 5309463..d6986e0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,27 +1,6 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - async: - dependency: transitive - description: - name: async - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.2.0" - boolean_selector: - dependency: transitive - description: - name: boolean_selector - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.0.4" - charcode: - dependency: transitive - description: - name: charcode - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.1.2" collection: dependency: transitive description: @@ -34,18 +13,6 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_test: - dependency: "direct dev" - description: flutter - source: sdk - version: "0.0.0" - matcher: - dependency: transitive - description: - name: matcher - url: "https://pub.flutter-io.cn" - source: hosted - version: "0.12.5" meta: dependency: transitive description: @@ -53,74 +20,11 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "1.1.6" - path: - dependency: transitive - description: - name: path - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.6.2" - pedantic: - dependency: transitive - description: - name: pedantic - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.7.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.0.3" sky_engine: dependency: transitive description: flutter source: sdk version: "0.0.99" - source_span: - dependency: transitive - description: - name: source_span - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.5.5" - stack_trace: - dependency: transitive - description: - name: stack_trace - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.9.3" - stream_channel: - dependency: transitive - description: - name: stream_channel - url: "https://pub.flutter-io.cn" - source: hosted - version: "2.0.0" - string_scanner: - dependency: transitive - description: - name: string_scanner - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.0.4" - term_glyph: - dependency: transitive - description: - name: term_glyph - url: "https://pub.flutter-io.cn" - source: hosted - version: "1.1.0" - test_api: - dependency: transitive - description: - name: test_api - url: "https://pub.flutter-io.cn" - source: hosted - version: "0.2.5" typed_data: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 8af0149..5b599f1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,8 +1,8 @@ name: flutter_gifimage -description: A new Flutter package project. +description: a gifimage for flutter,for solving gif cannot be controlled speed,progress version: 1.0.0 -author: -homepage: +author: Jpeng +homepage: https://github.com/peng8350/flutter_gifimage environment: sdk: ">=2.1.0 <3.0.0"