Skip to content
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

feat: support rive #143

Merged
merged 7 commits into from
Oct 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ flutter_gen:
integrations:
flutter_svg: true
flare_flutter: true
rive: true

colors:
inputs:
Expand Down Expand Up @@ -160,6 +161,7 @@ flutter:
- assets/images/icons/dart@test.svg
- assets/json/fruits.json
- assets/flare/Penguin.flr
- assets/rive/vehicles.riv
- pictures/ocean_view.jpg
```

Expand Down Expand Up @@ -222,6 +224,7 @@ Widget build(BuildContext context) {
|--|--|--|--|
|[flutter_svg](https://pub.dev/packages/flutter_svg)|.svg| `flutter_svg: true` |Assets.images.icons.paint.**svg()**|
|[flare_flutter](https://pub.dev/packages/flare_flutter)|.flr| `flare_flutter: true` |Assets.flare.penguin.**flare()**|
|[rive](https://pub.dev/packages/rive)|.flr| `rive: true` |Assets.rive.vehicles.**rive()**|

<br/>

Expand Down Expand Up @@ -279,6 +282,7 @@ import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter/services.dart';
import 'package:flare_flutter/flare_actor.dart';
import 'package:flare_flutter/flare_controller.dart';
import 'package:rive/rive.dart';

class $PicturesGen {
const $PicturesGen();
Expand Down Expand Up @@ -496,6 +500,38 @@ class FlareGenImage {
String get path => _assetName;
}

class RiveGenImage {
const RiveGenImage(this._assetName);

final String _assetName;

RiveAnimation rive({
String? artboard,
List<String> animations = const [],
List<String> stateMachines = const [],
BoxFit? fit,
Alignment? alignment,
Widget? placeHolder,
bool antialiasing = true,
List<RiveAnimationController> controllers = const [],
OnInitCallback? onInit,
}) {
return RiveAnimation.asset(
_assetName,
artboard: artboard,
animations: animations,
stateMachines: stateMachines,
fit: fit,
alignment: alignment,
placeHolder: placeHolder,
antialiasing: antialiasing,
controllers: controllers,
onInit: onInit,
);
}

String get path => _assetName;
}
```

</p>
Expand Down Expand Up @@ -684,6 +720,7 @@ flutter_gen:
integrations:
flutter_svg: false
flare_flutter: false
rive: false

assets:
# Optional
Expand Down
Binary file added example/assets/rive/vehicles.riv
Binary file not shown.
42 changes: 42 additions & 0 deletions example/lib/gen/assets.gen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter/services.dart';
import 'package:flare_flutter/flare_actor.dart';
import 'package:flare_flutter/flare_controller.dart';
import 'package:rive/rive.dart';

class $PicturesGen {
const $PicturesGen();
Expand Down Expand Up @@ -64,6 +65,13 @@ class $AssetsMovieGen {
String get theEarth => 'assets/movie/the_earth.mp4';
}

class $AssetsRiveGen {
const $AssetsRiveGen();

/// File path: assets/rive/vehicles.riv
RiveGenImage get vehicles => const RiveGenImage('assets/rive/vehicles.riv');
}

class $AssetsUnknownGen {
const $AssetsUnknownGen();

Expand Down Expand Up @@ -118,6 +126,7 @@ class Assets {
static const $AssetsImagesGen images = $AssetsImagesGen();
static const $AssetsJsonGen json = $AssetsJsonGen();
static const $AssetsMovieGen movie = $AssetsMovieGen();
static const $AssetsRiveGen rive = $AssetsRiveGen();
static const $AssetsUnknownGen unknown = $AssetsUnknownGen();
static const $PicturesGen pictures = $PicturesGen();
}
Expand Down Expand Up @@ -256,3 +265,36 @@ class FlareGenImage {

String get path => _assetName;
}

class RiveGenImage {
const RiveGenImage(this._assetName);

final String _assetName;

RiveAnimation rive({
String? artboard,
List<String> animations = const [],
List<String> stateMachines = const [],
BoxFit? fit,
Alignment? alignment,
Widget? placeHolder,
bool antialiasing = true,
List<RiveAnimationController> controllers = const [],
OnInitCallback? onInit,
}) {
return RiveAnimation.asset(
_assetName,
artboard: artboard,
animations: animations,
stateMachines: stateMachines,
fit: fit,
alignment: alignment,
placeHolder: placeHolder,
antialiasing: antialiasing,
controllers: controllers,
onInit: onInit,
);
}

String get path => _assetName;
}
7 changes: 7 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ void main() {
fit: BoxFit.contain,
),
),
SizedBox(
width: 200,
height: 200,
child: Assets.rive.vehicles.rive(
fit: BoxFit.contain,
),
),
Image(image: Assets.images.chip1),
Assets.images.icons.kmm.svg(key: const Key("kmm_svg")),
Assets.images.icons.fuchsia.svg(),
Expand Down
22 changes: 18 additions & 4 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ packages:
source: sdk
version: "0.0.0"
flutter_gen_core:
dependency: transitive
dependency: "direct overridden"
description:
name: flutter_gen_core
url: "https://pub.dartlang.org"
source: hosted
path: "../packages/core"
relative: true
source: path
version: "4.0.1"
flutter_gen_runner:
dependency: "direct dev"
Expand Down Expand Up @@ -263,6 +263,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
http:
dependency: transitive
description:
name: http
url: "https://pub.dartlang.org"
source: hosted
version: "0.13.4"
http_multi_server:
dependency: transitive
description:
Expand Down Expand Up @@ -396,6 +403,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
rive:
dependency: "direct main"
description:
name: rive
url: "https://pub.dartlang.org"
source: hosted
version: "0.7.32"
shelf:
dependency: transitive
description:
Expand Down
9 changes: 9 additions & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies:

flutter_svg: ^0.22.0
flare_flutter: ^3.0.2
rive: ^0.7.22

dev_dependencies:
flutter_test:
Expand All @@ -32,9 +33,11 @@ flutter_gen:
integrations:
flutter_svg: true
flare_flutter: true
rive: true

assets:
enabled: true
package_parameter_enabled: false
# Assets.imagesChip
# style: camel-case

Expand Down Expand Up @@ -68,6 +71,7 @@ flutter:
- assets/json/fruits.json
- pictures/chip5.jpg
- assets/flare/
- assets/rive/
- assets/movie/
- assets/unknown/
fonts:
Expand All @@ -89,3 +93,8 @@ flutter:
- asset: assets/fonts/RobotoMono-Bold.ttf
weight: 700

dependency_overrides:
flutter_gen_runner:
path: ../packages/runner
flutter_gen_core:
path: ../packages/core
2 changes: 2 additions & 0 deletions packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import 'generator_helper.dart';
import 'integrations/flare_integration.dart';
import 'integrations/integration.dart';
import 'integrations/svg_integration.dart';
import 'integrations/rive_integration.dart';

class AssetsGenConfig {
AssetsGenConfig._(
Expand Down Expand Up @@ -58,6 +59,7 @@ String generateAssets(
if (config.flutterGen.integrations.flutterSvg)
SvgIntegration(config.packageParameterLiteral),
if (config.flutterGen.integrations.flareFlutter) FlareIntegration(),
if (config.flutterGen.integrations.rive) RiveIntegration(),
];

if (config.flutterGen.assets.isDotDelimiterStyle) {
Expand Down
57 changes: 57 additions & 0 deletions packages/core/lib/generators/integrations/rive_integration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import '../../settings/asset_type.dart';
import 'integration.dart';

class RiveIntegration extends Integration {
@override
List<String> get requiredImports => [
'package:rive/rive.dart',
];

@override
String get classOutput => _classDefinition;

final String _classDefinition = '''class RiveGenImage {
const RiveGenImage(this._assetName);

final String _assetName;

RiveAnimation rive({
String? artboard,
List<String> animations = const [],
List<String> stateMachines = const [],
BoxFit? fit,
Alignment? alignment,
Widget? placeHolder,
bool antialiasing = true,
List<RiveAnimationController> controllers = const [],
OnInitCallback? onInit,
}) {
return RiveAnimation.asset(
_assetName,
artboard: artboard,
animations: animations,
stateMachines: stateMachines,
fit: fit,
alignment: alignment,
placeHolder: placeHolder,
antialiasing: antialiasing,
controllers: controllers,
onInit: onInit,
);
}

String get path => _assetName;
}''';

@override
String get className => 'RiveGenImage';

@override
String classInstantiate(String path) => 'RiveGenImage(\'$path\')';

@override
bool isSupport(AssetType type) => type.extension == '.riv';

@override
bool get isConstConstructor => true;
}
1 change: 1 addition & 0 deletions packages/core/lib/settings/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ flutter_gen:
integrations:
flutter_svg: false
flare_flutter: false
rive: false

assets:
enabled: true
Expand Down
10 changes: 8 additions & 2 deletions packages/core/lib/settings/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,21 @@ class FlutterGenFonts {

@JsonSerializable()
class FlutterGenIntegrations {
FlutterGenIntegrations(
{required this.flutterSvg, required this.flareFlutter});
FlutterGenIntegrations({
required this.flutterSvg,
required this.flareFlutter,
required this.rive,
});

@JsonKey(name: 'flutter_svg', required: true)
final bool flutterSvg;

@JsonKey(name: 'flare_flutter', required: true)
final bool flareFlutter;

@JsonKey(name: 'rive', required: true)
final bool rive;

factory FlutterGenIntegrations.fromJson(Map json) =>
_$FlutterGenIntegrationsFromJson(json);
}
3 changes: 2 additions & 1 deletion packages/core/lib/settings/pubspec.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions packages/core/test/assets_gen_integrations_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@TestOn('vm')
import 'package:flutter_gen_core/generators/integrations/flare_integration.dart';
import 'package:flutter_gen_core/generators/integrations/rive_integration.dart';
import 'package:flutter_gen_core/generators/integrations/svg_integration.dart';
import 'package:flutter_gen_core/settings/asset_type.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -52,5 +53,23 @@ void main() {
expect(integration.isSupport(AssetType('assets/path/dog.json')), isFalse);
expect(integration.isConstConstructor, isTrue);
});

test('Assets with Rive integrations on pubspec.yaml', () async {
const pubspec = 'test_resources/pubspec_assets_rive_integrations.yaml';
const fact =
'test_resources/actual_data/assets_rive_integrations.gen.dart';
const generated =
'test_resources/lib/gen/assets_rive_integrations.gen.dart';

await expectedAssetsGen(pubspec, generated, fact);

final integration = RiveIntegration();
expect(integration.className, 'RiveGenImage');
expect(integration.classInstantiate('assets/path'),
'RiveGenImage(\'assets/path\')');
expect(integration.isSupport(AssetType('assets/path/dog.riv')), isTrue);
expect(integration.isSupport(AssetType('assets/path/dog.json')), isFalse);
expect(integration.isConstConstructor, isTrue);
});
});
}
Loading