-
-
Notifications
You must be signed in to change notification settings - Fork 168
Add new option parse_animation
to parse metadata for animated images
#676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
4c279dd
0452cb3
b4de9b5
c126a82
4bea974
28e94a2
ebf5cae
809be17
010cae8
a51a9ed
2e2ae46
a565080
fa641bd
bbea3af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:flutter_gen_core/generators/integrations/integration.dart'; | ||
import 'package:image/image.dart' as img; | ||
import 'package:image_size_getter/file_input.dart'; | ||
import 'package:image_size_getter/image_size_getter.dart'; | ||
|
||
|
@@ -11,9 +12,12 @@ import 'package:image_size_getter/image_size_getter.dart'; | |
class ImageIntegration extends Integration { | ||
ImageIntegration( | ||
String packageName, { | ||
required this.parseAnimation, | ||
huandu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
super.parseMetadata, | ||
}) : super(packageName); | ||
|
||
final bool parseAnimation; | ||
|
||
String get packageParameter => isPackage ? ' = package' : ''; | ||
|
||
String get keyName => | ||
|
@@ -32,6 +36,9 @@ class ImageIntegration extends Integration { | |
this._assetName, { | ||
this.size, | ||
this.flavors = const {}, | ||
this.isAnimation = false, | ||
this.duration = Duration.zero, | ||
this.frames = 1, | ||
huandu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
|
||
final String _assetName; | ||
|
@@ -40,6 +47,9 @@ ${isPackage ? "\n static const String package = '$packageName';" : ''} | |
|
||
final Size? size; | ||
final Set<String> flavors; | ||
final bool isAnimation; | ||
final Duration duration; | ||
final int frames; | ||
|
||
Image image({ | ||
Key? key, | ||
|
@@ -116,12 +126,20 @@ ${isPackage ? "\n static const String package = '$packageName';" : ''} | |
|
||
@override | ||
String classInstantiate(AssetType asset) { | ||
final info = parseMetadata ? _getMetadata(asset) : null; | ||
final info = parseMetadata || parseAnimation ? _getMetadata(asset) : null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if the user puts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we go further and let |
||
final buffer = StringBuffer(className); | ||
buffer.write('('); | ||
buffer.write('\'${asset.posixStylePath}\''); | ||
if (info != null) { | ||
buffer.write(', size: Size(${info.width}, ${info.height})'); | ||
buffer.write(', size: const Size(${info.width}, ${info.height})'); | ||
|
||
if (info.animation case final animation?) { | ||
buffer.write(', isAnimation: ${animation.frames > 1}'); | ||
buffer.write( | ||
', duration: Duration(milliseconds: ${animation.duration.inMilliseconds})', | ||
); | ||
buffer.write(', frames: ${animation.frames}'); | ||
} | ||
} | ||
if (asset.flavors.isNotEmpty) { | ||
buffer.write(', flavors: {'); | ||
|
@@ -161,11 +179,47 @@ ${isPackage ? "\n static const String package = '$packageName';" : ''} | |
FileInput(File(asset.fullPath)), | ||
); | ||
final size = result.size; | ||
return ImageMetadata(size.width.toDouble(), size.height.toDouble()); | ||
final animation = parseAnimation ? _parseAnimation(asset) : null; | ||
|
||
return ImageMetadata( | ||
width: size.width.toDouble(), | ||
height: size.height.toDouble(), | ||
animation: animation, | ||
); | ||
} catch (e) { | ||
stderr | ||
.writeln('[WARNING] Failed to parse \'${asset.path}\' metadata: $e'); | ||
} | ||
return null; | ||
} | ||
|
||
ImageAnimation? _parseAnimation(AssetType asset) { | ||
final decoder = switch (asset.mime) { | ||
'image/gif' => img.GifDecoder(), | ||
'image/webp' => img.WebPDecoder(), | ||
_ => null, | ||
}; | ||
|
||
if (decoder == null) { | ||
return null; | ||
} | ||
|
||
final file = File(asset.fullPath); | ||
final bytes = file.readAsBytesSync(); | ||
final image = decoder.decode(bytes); | ||
huandu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (image == null) { | ||
return null; | ||
} | ||
|
||
return ImageAnimation( | ||
frames: image.frames.length, | ||
duration: Duration( | ||
milliseconds: image.frames.fold( | ||
0, | ||
(duration, frame) => duration + frame.frameDuration, | ||
), | ||
), | ||
); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
/// DO NOT MODIFY BY HAND, Generated by version_gen | ||
String packageVersion = '5.10.0'; | ||
String packageVersion = '5.11.0'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.