From c13ebacf7b8cd0fa54e2360d78e141569914d928 Mon Sep 17 00:00:00 2001 From: Nolence Date: Thu, 23 Jun 2022 16:06:09 -0600 Subject: [PATCH] formatting, linting and remove warnings from flutter v3 --- analysis_options.yaml | 36 +++++++ example/analysis_options copy.yaml | 36 +++++++ example/lib/main.dart | 10 +- example/pubspec.lock | 27 ++---- example/pubspec.yaml | 11 +-- .../windows/flutter/generated_plugins.cmake | 8 ++ lib/flutter_gif.dart | 96 ++++++++++++------- pubspec.lock | 27 ++---- pubspec.yaml | 4 +- 9 files changed, 170 insertions(+), 85 deletions(-) create mode 100644 analysis_options.yaml create mode 100644 example/analysis_options copy.yaml diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 0000000..44bc250 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,36 @@ +include: package:flutter_lints/flutter.yaml + +analyzer: + errors: + todo: ignore + strong-mode: + implicit-casts: false + implicit-dynamic: false + +linter: + rules: + camel_case_types: true + require_trailing_commas: true + avoid_positional_boolean_parameters: false + avoid_print: true + always_declare_return_types: true + prefer_is_empty: true + prefer_is_not_empty: true + avoid_empty_else: true + avoid_type_to_string: true + close_sinks: true + control_flow_in_finally: true + empty_statements: true + throw_in_finally: true + unawaited_futures: true + unnecessary_await_in_return: true + avoid_shadowing_type_parameters: true + parameter_assignments: true + only_throw_errors: true + always_use_package_imports: true + avoid_dynamic_calls: true + prefer_void_to_null: true + unnecessary_lambdas: true + unnecessary_const: true + avoid_void_async: true + lines_longer_than_80_chars: true diff --git a/example/analysis_options copy.yaml b/example/analysis_options copy.yaml new file mode 100644 index 0000000..44bc250 --- /dev/null +++ b/example/analysis_options copy.yaml @@ -0,0 +1,36 @@ +include: package:flutter_lints/flutter.yaml + +analyzer: + errors: + todo: ignore + strong-mode: + implicit-casts: false + implicit-dynamic: false + +linter: + rules: + camel_case_types: true + require_trailing_commas: true + avoid_positional_boolean_parameters: false + avoid_print: true + always_declare_return_types: true + prefer_is_empty: true + prefer_is_not_empty: true + avoid_empty_else: true + avoid_type_to_string: true + close_sinks: true + control_flow_in_finally: true + empty_statements: true + throw_in_finally: true + unawaited_futures: true + unnecessary_await_in_return: true + avoid_shadowing_type_parameters: true + parameter_assignments: true + only_throw_errors: true + always_use_package_imports: true + avoid_dynamic_calls: true + prefer_void_to_null: true + unnecessary_lambdas: true + unnecessary_const: true + avoid_void_async: true + lines_longer_than_80_chars: true diff --git a/example/lib/main.dart b/example/lib/main.dart index b8e0f2f..f7e5d8d 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -13,9 +13,7 @@ class MyApp extends StatelessWidget { Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', - theme: ThemeData( - primarySwatch: Colors.blue, - ), + theme: ThemeData(primarySwatch: Colors.blue), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } @@ -26,7 +24,7 @@ class MyHomePage extends StatefulWidget { final String title; @override - _MyHomePageState createState() => _MyHomePageState(); + State createState() => _MyHomePageState(); } class _MyHomePageState extends State with TickerProviderStateMixin { @@ -37,14 +35,14 @@ class _MyHomePageState extends State with TickerProviderStateMixin { controller1 = FlutterGifController(vsync: this); controller2 = FlutterGifController(vsync: this); controller4 = FlutterGifController(vsync: this); - WidgetsBinding.instance?.addPostFrameCallback((_) { + WidgetsBinding.instance.addPostFrameCallback((_) { controller1.repeat( min: 0, max: 53, period: const Duration(milliseconds: 200), ); }); - WidgetsBinding.instance?.addPostFrameCallback((_) { + WidgetsBinding.instance.addPostFrameCallback((_) { controller2.repeat( min: 0, max: 13, diff --git a/example/pubspec.lock b/example/pubspec.lock index 7bb5165..d08dc99 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -42,7 +42,7 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" cupertino_icons: dependency: "direct main" description: @@ -56,7 +56,7 @@ packages: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" flutter: dependency: "direct main" description: flutter @@ -75,7 +75,7 @@ packages: name: flutter_lints url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.0.1" flutter_test: dependency: "direct dev" description: flutter @@ -87,7 +87,7 @@ packages: name: lints url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "2.0.0" matcher: dependency: transitive description: @@ -101,7 +101,7 @@ packages: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.1.3" + version: "0.1.4" meta: dependency: transitive description: @@ -115,7 +115,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" sky_engine: dependency: transitive description: flutter @@ -127,7 +127,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -162,21 +162,14 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.8" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.9" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" + version: "2.1.2" sdks: - dart: ">=2.16.1 <3.0.0" + dart: ">=2.17.1 <3.0.0" flutter: ">=2.5.0" diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 8864918..b594ad3 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -10,25 +10,22 @@ environment: dependencies: flutter: sdk: flutter - + cupertino_icons: ^1.0.4 flutter_gif: - path: ../ + path: ../ dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^1.0.0 - - + flutter_lints: ^2.0.0 # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec # The following section is specific to Flutter. flutter: - # The following line ensures that the Material Icons font is # included with your application, so that you can use the icons in # the material Icons class. @@ -36,7 +33,7 @@ flutter: # To add assets to your application, add an assets section, like this: assets: - - images/animate.gif + - images/animate.gif # - images/a_dot_ham.jpeg # An image asset can refer to one or more resolution-specific "variants", see diff --git a/example/windows/flutter/generated_plugins.cmake b/example/windows/flutter/generated_plugins.cmake index 4d10c25..b93c4c3 100644 --- a/example/windows/flutter/generated_plugins.cmake +++ b/example/windows/flutter/generated_plugins.cmake @@ -5,6 +5,9 @@ list(APPEND FLUTTER_PLUGIN_LIST ) +list(APPEND FLUTTER_FFI_PLUGIN_LIST +) + set(PLUGIN_BUNDLED_LIBRARIES) foreach(plugin ${FLUTTER_PLUGIN_LIST}) @@ -13,3 +16,8 @@ foreach(plugin ${FLUTTER_PLUGIN_LIST}) list(APPEND PLUGIN_BUNDLED_LIBRARIES $) list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${plugin}_bundled_libraries}) endforeach(plugin) + +foreach(ffi_plugin ${FLUTTER_FFI_PLUGIN_LIST}) + add_subdirectory(flutter/ephemeral/.plugin_symlinks/${ffi_plugin}/windows plugins/${ffi_plugin}) + list(APPEND PLUGIN_BUNDLED_LIBRARIES ${${ffi_plugin}_bundled_libraries}) +endforeach(ffi_plugin) diff --git a/lib/flutter_gif.dart b/lib/flutter_gif.dart index 867176d..9ffd781 100644 --- a/lib/flutter_gif.dart +++ b/lib/flutter_gif.dart @@ -1,20 +1,22 @@ /* author: Saytoonz email: saytoonz05@gmail.com - time:2022-04-18 09:54 + time: 2022-04-18 09:54 */ library flutter_gif; import 'dart:io'; +import 'dart:typed_data'; 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(); + final caches = >{}; void clear() { caches.clear(); @@ -22,27 +24,30 @@ class GifCache { bool evict(Object key) { final List? pendingImage = caches.remove(key); + if (pendingImage != null) { return true; } + return false; } } /// control gif class FlutterGifController extends AnimationController { - FlutterGifController( - {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); + FlutterGifController({ + 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() { @@ -51,7 +56,8 @@ class FlutterGifController extends AnimationController { } class GifImage extends StatefulWidget { - GifImage({ + const GifImage({ + super.key, required this.image, required this.controller, this.semanticLabel, @@ -68,6 +74,7 @@ class GifImage extends StatefulWidget { this.matchTextDirection = false, this.gaplessPlayback = false, }); + final VoidCallback? onFetchCompleted; final FlutterGifController controller; final ImageProvider image; @@ -86,7 +93,7 @@ class GifImage extends StatefulWidget { @override State createState() { - return new GifImageState(); + return GifImageState(); } static GifCache cache = GifCache(); @@ -96,6 +103,7 @@ 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]; @@ -111,6 +119,7 @@ class GifImageState extends State { void dispose() { super.dispose(); widget.controller.removeListener(_listener); + widget.controller.dispose(); } @override @@ -118,7 +127,7 @@ class GifImageState extends State { super.didUpdateWidget(oldWidget); if (widget.image != oldWidget.image) { fetchGif(widget.image).then((imageInfors) { - if (mounted) + if (mounted) { setState(() { _infos = imageInfors; _fetchComplete = true; @@ -127,8 +136,10 @@ class GifImageState extends State { widget.onFetchCompleted!(); } }); + } }); } + if (widget.controller != oldWidget.controller) { oldWidget.controller.removeListener(_listener); widget.controller.addListener(_listener); @@ -137,10 +148,11 @@ class GifImageState extends State { void _listener() { if (_curIndex != widget.controller.value && _fetchComplete) { - if (mounted) + if (mounted) { setState(() { _curIndex = widget.controller.value.toInt(); }); + } } } @@ -149,7 +161,7 @@ class GifImageState extends State { super.didChangeDependencies(); if (_infos == null) { fetchGif(widget.image).then((imageInfors) { - if (mounted) + if (mounted) { setState(() { _infos = imageInfors; _fetchComplete = true; @@ -158,13 +170,14 @@ class GifImageState extends State { widget.onFetchCompleted!(); } }); + } }); } } @override Widget build(BuildContext context) { - final RawImage image = new RawImage( + final image = RawImage( image: _imageInfo?.image, width: widget.width, height: widget.height, @@ -177,11 +190,13 @@ class GifImageState extends State { centerSlice: widget.centerSlice, matchTextDirection: widget.matchTextDirection, ); + if (widget.excludeFromSemantics) return image; - return new Semantics( + + return Semantics( container: widget.semanticLabel != null, image: true, - label: widget.semanticLabel == null ? '' : widget.semanticLabel, + label: widget.semanticLabel ?? '', child: image, ); } @@ -191,28 +206,35 @@ final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false; HttpClient get _httpClient { HttpClient client = _sharedHttpClient; - assert(() { - if (debugNetworkImageHttpClientProvider != null) - client = debugNetworkImageHttpClientProvider!(); - return true; - }()); + + assert( + () { + if (debugNetworkImageHttpClientProvider != null) { + client = debugNetworkImageHttpClientProvider!(); + } + return true; + }(), + ); + return client; } Future> fetchGif(ImageProvider provider) async { List infos = []; - dynamic data; - String key = provider is NetworkImage + final Uint8List bytes; + final 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); @@ -220,20 +242,22 @@ Future> fetchGif(ImageProvider provider) async { request.headers.add(name, value); }); final HttpClientResponse response = await request.close(); - data = await consolidateHttpClientResponseBytes( + bytes = await consolidateHttpClientResponseBytes( response, ); } else if (provider is AssetImage) { - AssetBundleImageKey key = await provider.obtainKey(ImageConfiguration()); - data = await key.bundle.load(key.name); + AssetBundleImageKey key = + await provider.obtainKey(const ImageConfiguration()); + bytes = (await key.bundle.load(key.name)).buffer.asUint8List(); } else if (provider is FileImage) { - data = await provider.file.readAsBytes(); + bytes = await provider.file.readAsBytes(); } else if (provider is MemoryImage) { - data = provider.bytes; + bytes = provider.bytes; + } else { + throw Exception("Unsupported image provider"); } - ui.Codec codec = await PaintingBinding.instance! - .instantiateImageCodec(data.buffer.asUint8List()); + ui.Codec codec = await PaintingBinding.instance.instantiateImageCodec(bytes); infos = []; for (int i = 0; i < codec.frameCount; i++) { FrameInfo frameInfo = await codec.getNextFrame(); diff --git a/pubspec.lock b/pubspec.lock index 76052d3..70ffced 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -42,14 +42,14 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.15.0" + version: "1.16.0" fake_async: dependency: transitive description: name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "1.2.0" + version: "1.3.0" flutter: dependency: "direct main" description: flutter @@ -61,7 +61,7 @@ packages: name: flutter_lints url: "https://pub.dartlang.org" source: hosted - version: "1.0.4" + version: "2.0.1" flutter_test: dependency: "direct dev" description: flutter @@ -73,7 +73,7 @@ packages: name: lints url: "https://pub.dartlang.org" source: hosted - version: "1.0.1" + version: "2.0.0" matcher: dependency: transitive description: @@ -87,7 +87,7 @@ packages: name: material_color_utilities url: "https://pub.dartlang.org" source: hosted - version: "0.1.3" + version: "0.1.4" meta: dependency: transitive description: @@ -101,7 +101,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "1.8.1" sky_engine: dependency: transitive description: flutter @@ -113,7 +113,7 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.8.1" + version: "1.8.2" stack_trace: dependency: transitive description: @@ -148,21 +148,14 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.8" - typed_data: - dependency: transitive - description: - name: typed_data - url: "https://pub.dartlang.org" - source: hosted - version: "1.3.0" + version: "0.4.9" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" + version: "2.1.2" sdks: - dart: ">=2.16.1 <3.0.0" + dart: ">=2.17.1 <3.0.0" flutter: ">=2.5.0" diff --git a/pubspec.yaml b/pubspec.yaml index c50aafc..42a73b6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ version: 0.0.4 homepage: https://github.com/saytoonz/flutter_gif environment: - sdk: ">=2.16.1 <3.0.0" + sdk: ">=2.17.1 <3.0.0" flutter: ">=2.5.0" dependencies: @@ -14,4 +14,4 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^1.0.0 \ No newline at end of file + flutter_lints: ^2.0.0