Skip to content

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

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fluttergen -c example/pubspec.yaml
## Configuration file

[FlutterGen] generates dart files based on the key **`flutter`** and **`flutter_gen`** of [`pubspec.yaml`](https://dart.dev/tools/pub/pubspec).
Default configuration can be found [here](https://github.com/FlutterGen/flutter_gen/tree/main/packages/core/lib/settings/config_default.dart).
Default configuration can be found [here](https://github.com/FlutterGen/flutter_gen/tree/main/packages/core/lib/settings/config_default.dart).

```yaml
# pubspec.yaml
Expand Down Expand Up @@ -173,7 +173,6 @@ flutter:

You can also configure generate options in the `build.yaml`, it will be read before the `pubspec.yaml` if it exists.


```yaml
# build.yaml
# ...
Expand Down Expand Up @@ -276,7 +275,9 @@ Widget build(BuildContext context) {
);
}
```

or

```dart
// Explicit usage for `Image`/`SvgPicture`/`Lottie`.
Widget build(BuildContext context) {
Expand Down Expand Up @@ -340,6 +341,32 @@ Widget build(BuildContext context) {
}
```

You can use `parse_animation` to generate more animation details.
It will automatically parse all animation information for GIF and WebP files,
including frames, duration, etc. As this option significantly increases generation time,
The option is disabled by default; enabling it will significantly increase the generation elapse.

```yaml
flutter_gen:
images:
parse_animation: true # <- Add this line (default: false)
# This option implies parse_metadata: true when parsing images.
```

For GIF and WebP animation, several new nullable field is added to the
generated class. For example:

```dart
AssetGenImage get animated =>
const AssetGenImage(
'assets/images/animated.webp',
size: Size(209.0, 49.0),
isAnimation: true,
duration: Duration(milliseconds: 1000),
frames: 15,
);
```

#### Usage Example

[FlutterGen] generates [Image](https://api.flutter.dev/flutter/widgets/Image-class.html) class if the asset is Flutter supported image format.
Expand Down Expand Up @@ -575,7 +602,9 @@ Plugin issues that are not specific to [FlutterGen] can be filed in the [Flutter
### Known Issues

#### Bad State: No Element when using build_runner

If you get an error message like this:

```
[SEVERE] flutter_gen_runner:flutter_gen_runner on $package$:

Expand Down Expand Up @@ -614,8 +643,6 @@ output-localization-file: app_localizations.dart
synthetic-package: false <--- ⚠️Add this line⚠️
```

If you get

## Contributing

**We are looking for co-developers.**
Expand Down
67 changes: 22 additions & 45 deletions examples/example/lib/gen/assets.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ class $AssetsImagesGen {
const AssetGenImage('assets/images/profile.png');

/// List of all assets
List<AssetGenImage> get values =>
[chip1, chip2, logo, profileJpg, profilePng];
List<AssetGenImage> get values => [
chip1,
chip2,
logo,
profileJpg,
profilePng,
];
}

class $AssetsJsonGen {
Expand Down Expand Up @@ -107,7 +112,7 @@ class $AssetsLottieGen {
cat,
geometricalAnimation,
hamburgerArrow,
spinningCarrousel
spinningCarrousel,
];
}

Expand Down Expand Up @@ -175,10 +180,8 @@ class $AssetsImagesChip4Gen {
const $AssetsImagesChip4Gen();

/// File path: assets/images/chip4/chip4.jpg
AssetGenImage get chip4 => const AssetGenImage(
'assets/images/chip4/chip4.jpg',
flavors: {'extern'},
);
AssetGenImage get chip4 =>
const AssetGenImage('assets/images/chip4/chip4.jpg', flavors: {'extern'});

/// List of all assets
List<AssetGenImage> get values => [chip4];
Expand Down Expand Up @@ -236,11 +239,7 @@ class MyAssets {
}

class AssetGenImage {
const AssetGenImage(
this._assetName, {
this.size,
this.flavors = const {},
});
const AssetGenImage(this._assetName, {this.size, this.flavors = const {}});

final String _assetName;

Expand Down Expand Up @@ -300,15 +299,8 @@ class AssetGenImage {
);
}

ImageProvider provider({
AssetBundle? bundle,
String? package,
}) {
return AssetImage(
_assetName,
bundle: bundle,
package: package,
);
ImageProvider provider({AssetBundle? bundle, String? package}) {
return AssetImage(_assetName, bundle: bundle, package: package);
}

String get path => _assetName;
Expand All @@ -317,17 +309,11 @@ class AssetGenImage {
}

class SvgGenImage {
const SvgGenImage(
this._assetName, {
this.size,
this.flavors = const {},
}) : _isVecFormat = false;

const SvgGenImage.vec(
this._assetName, {
this.size,
this.flavors = const {},
}) : _isVecFormat = true;
const SvgGenImage(this._assetName, {this.size, this.flavors = const {}})
: _isVecFormat = false;

const SvgGenImage.vec(this._assetName, {this.size, this.flavors = const {}})
: _isVecFormat = true;

final String _assetName;
final Size? size;
Expand Down Expand Up @@ -394,10 +380,7 @@ class SvgGenImage {
}

class RiveGenImage {
const RiveGenImage(
this._assetName, {
this.flavors = const {},
});
const RiveGenImage(this._assetName, {this.flavors = const {}});

final String _assetName;
final Set<String> flavors;
Expand Down Expand Up @@ -435,10 +418,7 @@ class RiveGenImage {
}

class LottieGenImage {
const LottieGenImage(
this._assetName, {
this.flavors = const {},
});
const LottieGenImage(this._assetName, {this.flavors = const {}});

final String _assetName;
final Set<String> flavors;
Expand All @@ -455,11 +435,8 @@ class LottieGenImage {
_lottie.LottieImageProviderFactory? imageProviderFactory,
Key? key,
AssetBundle? bundle,
Widget Function(
BuildContext,
Widget,
_lottie.LottieComposition?,
)? frameBuilder,
Widget Function(BuildContext, Widget, _lottie.LottieComposition?)?
frameBuilder,
ImageErrorWidgetBuilder? errorBuilder,
double? width,
double? height,
Expand Down
72 changes: 33 additions & 39 deletions examples/example/lib/gen/colors.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,19 @@ class MyColorName {
/// 700: #FFC31F1F
/// 800: #FFBD1919
/// 900: #FFB20F0F
static const MaterialColor crimsonRed = MaterialColor(
0xFFCF2A2A,
<int, Color>{
50: Color(0xFFF9E5E5),
100: Color(0xFFF1BFBF),
200: Color(0xFFE79595),
300: Color(0xFFDD6A6A),
400: Color(0xFFD64A4A),
500: Color(0xFFCF2A2A),
600: Color(0xFFCA2525),
700: Color(0xFFC31F1F),
800: Color(0xFFBD1919),
900: Color(0xFFB20F0F),
},
);
static const MaterialColor crimsonRed =
MaterialColor(0xFFCF2A2A, <int, Color>{
50: Color(0xFFF9E5E5),
100: Color(0xFFF1BFBF),
200: Color(0xFFE79595),
300: Color(0xFFDD6A6A),
400: Color(0xFFD64A4A),
500: Color(0xFFCF2A2A),
600: Color(0xFFCA2525),
700: Color(0xFFC31F1F),
800: Color(0xFFBD1919),
900: Color(0xFFB20F0F),
});

/// Color: #979797
static const Color gray410 = Color(0xFF979797);
Expand All @@ -75,34 +73,30 @@ class MyColorName {
/// 700: #FFD7821D
/// 800: #FFD27817
/// 900: #FFCA670E
static const MaterialColor yellowOcher = MaterialColor(
0xFFDF9527,
<int, Color>{
50: Color(0xFFFBF2E5),
100: Color(0xFFF5DFBE),
200: Color(0xFFEFCA93),
300: Color(0xFFE9B568),
400: Color(0xFFE4A547),
500: Color(0xFFDF9527),
600: Color(0xFFDB8D23),
700: Color(0xFFD7821D),
800: Color(0xFFD27817),
900: Color(0xFFCA670E),
},
);
static const MaterialColor yellowOcher =
MaterialColor(0xFFDF9527, <int, Color>{
50: Color(0xFFFBF2E5),
100: Color(0xFFF5DFBE),
200: Color(0xFFEFCA93),
300: Color(0xFFE9B568),
400: Color(0xFFE4A547),
500: Color(0xFFDF9527),
600: Color(0xFFDB8D23),
700: Color(0xFFD7821D),
800: Color(0xFFD27817),
900: Color(0xFFCA670E),
});

/// MaterialAccentColor:
/// 100: #FFFFE8E0
/// 200: #FFFFBCA3
/// 400: #FFFFA989
/// 700: #FFFF9E7A
static const MaterialAccentColor yellowOcherAccent = MaterialAccentColor(
0xFFFFBCA3,
<int, Color>{
100: Color(0xFFFFE8E0),
200: Color(0xFFFFBCA3),
400: Color(0xFFFFA989),
700: Color(0xFFFF9E7A),
},
);
static const MaterialAccentColor yellowOcherAccent =
MaterialAccentColor(0xFFFFBCA3, <int, Color>{
100: Color(0xFFFFE8E0),
200: Color(0xFFFFBCA3),
400: Color(0xFFFFA989),
700: Color(0xFFFF9E7A),
});
}
Loading
Loading