Skip to content

⚡️ Improvements #643

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

Merged
merged 3 commits into from
Jan 29, 2025
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ This would add the package constant to the generated class. For example:

```dart
class Assets {
Assets._();
const Assets._();

static const String package = 'test';

Expand Down
4 changes: 3 additions & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ linter:
avoid_classes_with_only_static_members: true
directives_ordering: true
flutter_style_todos: true
prefer_int_literals: true
prefer_const_constructors: true
prefer_const_constructors_in_immutables: true
prefer_final_in_for_each: true
prefer_int_literals: true
prefer_single_quotes: true
# require_trailing_commas: true
# sort_constructors_first: true
Expand Down
46 changes: 27 additions & 19 deletions examples/example/test/svg_integrations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,42 @@ class SvgIntegrationsTest extends StatelessWidget {

void main() {
group('Test SvgTheme behavior', () {
var testTheme = SvgTheme(currentColor: Colors.red);
const testTheme = SvgTheme(currentColor: Colors.red);

testWidgets('Passed theme should be null', (widgetTester) async {
await widgetTester.pumpWidget(SvgIntegrationsTest(theme: testTheme));
testWidgets(
'Passed theme should be null',
(widgetTester) async {
await widgetTester.pumpWidget(
const SvgIntegrationsTest(theme: testTheme),
);

var finder = find.byType(SvgPicture);
expect(finder, findsOneWidget);
var finder = find.byType(SvgPicture);
expect(finder, findsOneWidget);

var svgWidget = widgetTester.widget<SvgPicture>(finder);
var loader = svgWidget.bytesLoader as SvgAssetLoader;
var svgWidget = widgetTester.widget<SvgPicture>(finder);
var loader = svgWidget.bytesLoader as SvgAssetLoader;

expect(loader.theme, isNull);
});
expect(loader.theme, isNull);
},
);

testWidgets(
'Taken theme of SvgAssetLoader equals with one passed to parent DefaultSvgTheme',
(widgetTester) async {
await widgetTester.pumpWidget(SvgIntegrationsTest(theme: testTheme));
'Taken theme of SvgAssetLoader equals with one passed to parent DefaultSvgTheme',
(widgetTester) async {
await widgetTester.pumpWidget(
const SvgIntegrationsTest(theme: testTheme),
);

var finder = find.byType(SvgPicture);
expect(finder, findsOneWidget);
var finder = find.byType(SvgPicture);
expect(finder, findsOneWidget);

var svgWidget = widgetTester.widget<SvgPicture>(finder);
var loader = svgWidget.bytesLoader as SvgAssetLoader;
var svgWidget = widgetTester.widget<SvgPicture>(finder);
var loader = svgWidget.bytesLoader as SvgAssetLoader;

var svgCacheKey = loader.cacheKey(widgetTester.element(finder));
var svgCacheKey = loader.cacheKey(widgetTester.element(finder));

expect(svgCacheKey.theme, testTheme);
});
expect(svgCacheKey.theme, testTheme);
},
);
});
}
30 changes: 18 additions & 12 deletions packages/core/lib/generators/assets_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,19 @@ Future<String> generateAssets(
}

final classesBuffer = StringBuffer();
if (config.flutterGen.assets.outputs.isDotDelimiterStyle) {
final definition = await _dotDelimiterStyleDefinition(config, integrations);
classesBuffer.writeln(definition);
} else if (config.flutterGen.assets.outputs.isSnakeCaseStyle) {
final definition = await _snakeCaseStyleDefinition(config, integrations);
classesBuffer.writeln(definition);
} else if (config.flutterGen.assets.outputs.isCamelCaseStyle) {
final definition = await _camelCaseStyleDefinition(config, integrations);
classesBuffer.writeln(definition);
} else {
throw 'The value of "flutter_gen/assets/style." is incorrect.';
final _StyleDefinition definition;
switch (config.flutterGen.assets.outputs.style) {
case FlutterGenElementAssetsOutputsStyle.dotDelimiterStyle:
definition = _dotDelimiterStyleDefinition;
break;
case FlutterGenElementAssetsOutputsStyle.snakeCaseStyle:
definition = _snakeCaseStyleDefinition;
break;
case FlutterGenElementAssetsOutputsStyle.camelCaseStyle:
definition = _camelCaseStyleDefinition;
break;
}
classesBuffer.writeln(await definition(config, integrations));

final imports = <Import>{};
for (final integration in integrations.where((e) => e.isEnabled)) {
Expand Down Expand Up @@ -400,6 +401,11 @@ Future<String> _dotDelimiterStyleDefinition(
return buffer.toString();
}

typedef _StyleDefinition = Future<String> Function(
AssetsGenConfig config,
List<Integration> integrations,
);

/// Generate style like Assets.foo_bar
Future<String> _snakeCaseStyleDefinition(
AssetsGenConfig config,
Expand Down Expand Up @@ -520,7 +526,7 @@ String _assetsClassDefinition(
) {
return '''
class $className {
$className._();
const $className._();
${packageName != null ? "\n static const String package = '$packageName';" : ''}

$statementsBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class ImageIntegration extends Integration {
isPackage ? "'packages/$packageName/\$_assetName'" : '_assetName';

@override
List<Import> get requiredImports => [Import('package:flutter/widgets.dart')];
List<Import> get requiredImports => const [
Import('package:flutter/widgets.dart'),
];

@override
String get classOutput => _classDefinition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class LottieIntegration extends Integration {
String get packageExpression => isPackage ? ' = package' : '';

@override
List<Import> get requiredImports => [
List<Import> get requiredImports => const [
Import('package:flutter/widgets.dart'),
Import('package:lottie/lottie.dart', alias: '_lottie'),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class RiveIntegration extends Integration {
String? get packageExpression => isPackage ? 'packages/$packageName/' : null;

@override
List<Import> get requiredImports => [
List<Import> get requiredImports => const [
Import('package:flutter/widgets.dart'),
Import('package:rive/rive.dart', alias: '_rive'),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SvgIntegration extends Integration {
String get packageExpression => isPackage ? ' = package' : '';

@override
List<Import> get requiredImports => [
List<Import> get requiredImports => const [
Import('package:flutter/widgets.dart'),
Import('package:flutter/services.dart'),
Import('package:flutter_svg/flutter_svg.dart', alias: '_svg'),
Expand Down
62 changes: 33 additions & 29 deletions packages/core/lib/settings/pubspec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ part 'pubspec.g.dart';

@JsonSerializable(disallowUnrecognizedKeys: false)
class Pubspec {
Pubspec({
const Pubspec({
required this.packageName,
required this.flutterGen,
required this.flutter,
Expand All @@ -26,7 +26,7 @@ class Pubspec {

@JsonSerializable(disallowUnrecognizedKeys: false)
class Flutter {
Flutter({
const Flutter({
required this.assets,
required this.fonts,
});
Expand All @@ -42,7 +42,7 @@ class Flutter {

@JsonSerializable(disallowUnrecognizedKeys: false)
class FlutterFonts {
FlutterFonts({required this.family});
const FlutterFonts({required this.family});

@JsonKey(name: 'family', required: true)
final String family;
Expand All @@ -52,7 +52,7 @@ class FlutterFonts {

@JsonSerializable()
class FlutterGen {
FlutterGen({
const FlutterGen({
required this.output,
required this.lineLength,
required this.parseMetadata,
Expand Down Expand Up @@ -88,7 +88,7 @@ class FlutterGen {

@JsonSerializable()
class FlutterGenColors {
FlutterGenColors({
const FlutterGenColors({
required this.enabled,
required this.inputs,
required this.outputs,
Expand All @@ -109,7 +109,7 @@ class FlutterGenColors {

@JsonSerializable()
class FlutterGenAssets {
FlutterGenAssets({
const FlutterGenAssets({
required this.enabled,
this.packageParameterEnabled,
this.style,
Expand Down Expand Up @@ -140,7 +140,7 @@ class FlutterGenAssets {

@JsonSerializable()
class FlutterGenFonts {
FlutterGenFonts({
const FlutterGenFonts({
required this.enabled,
required this.outputs,
});
Expand All @@ -156,7 +156,7 @@ class FlutterGenFonts {

@JsonSerializable()
class FlutterGenIntegrations {
FlutterGenIntegrations({
const FlutterGenIntegrations({
required this.flutterSvg,
required this.rive,
required this.lottie,
Expand All @@ -177,7 +177,7 @@ class FlutterGenIntegrations {

@JsonSerializable()
class FlutterGenElementOutputs {
FlutterGenElementOutputs({
const FlutterGenElementOutputs({
required this.className,
});

Expand All @@ -188,24 +188,34 @@ class FlutterGenElementOutputs {
_$FlutterGenElementOutputsFromJson(json);
}

enum FlutterGenElementAssetsOutputsStyle {
dotDelimiterStyle('dot-delimiter'),
snakeCaseStyle('snake-case'),
camelCaseStyle('camel-case'),
;

const FlutterGenElementAssetsOutputsStyle(this.name);

factory FlutterGenElementAssetsOutputsStyle.fromJson(String json) {
return values.firstWhere(
(e) => e.name == json,
orElse: () => throw ArgumentError.value(json, 'style'),
);
}

final String name;

String toJson() => name;
}

@JsonSerializable()
class FlutterGenElementAssetsOutputs extends FlutterGenElementOutputs {
static const String dotDelimiterStyle = 'dot-delimiter';
static const String snakeCaseStyle = 'snake-case';
static const String camelCaseStyle = 'camel-case';

FlutterGenElementAssetsOutputs({
const FlutterGenElementAssetsOutputs({
required String className,
required this.packageParameterEnabled,
required this.directoryPathEnabled,
required this.style,
}) : super(className: className) {
if (style != dotDelimiterStyle &&
style != snakeCaseStyle &&
style != camelCaseStyle) {
throw ArgumentError.value(style, 'style');
}
}
}) : super(className: className);

@JsonKey(name: 'package_parameter_enabled', defaultValue: false)
final bool packageParameterEnabled;
Expand All @@ -214,21 +224,15 @@ class FlutterGenElementAssetsOutputs extends FlutterGenElementOutputs {
final bool directoryPathEnabled;

@JsonKey(name: 'style', required: true)
final String style;

bool get isDotDelimiterStyle => style == dotDelimiterStyle;

bool get isSnakeCaseStyle => style == snakeCaseStyle;

bool get isCamelCaseStyle => style == camelCaseStyle;
final FlutterGenElementAssetsOutputsStyle style;

factory FlutterGenElementAssetsOutputs.fromJson(Map json) =>
_$FlutterGenElementAssetsOutputsFromJson(json);
}

@JsonSerializable()
class FlutterGenElementFontsOutputs extends FlutterGenElementOutputs {
FlutterGenElementFontsOutputs({
const FlutterGenElementFontsOutputs({
required super.className,
required this.packageParameterEnabled,
});
Expand Down
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.

Loading