Skip to content

Commit 47df1f8

Browse files
authored
✨ Add Import abstraction for package imports alias (#548)
## What does this change? Resolves #545 ## Type of change - [x] New feature (non-breaking change which adds functionality)
1 parent 313027f commit 47df1f8

22 files changed

+254
-211
lines changed

examples/example/lib/gen/assets.gen.dart

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
// ignore_for_file: type=lint
88
// ignore_for_file: directives_ordering,unnecessary_import,implicit_dynamic_list_literal,deprecated_member_use
99

10-
import 'package:flutter/widgets.dart';
10+
import 'package:flare_flutter/flare_actor.dart' as _flare_actor;
11+
import 'package:flare_flutter/flare_controller.dart' as _flare_controller;
1112
import 'package:flutter/services.dart';
12-
import 'package:flutter_svg/flutter_svg.dart';
13-
import 'package:vector_graphics/vector_graphics.dart';
14-
import 'package:flare_flutter/flare_actor.dart';
15-
import 'package:flare_flutter/flare_controller.dart';
16-
import 'package:rive/rive.dart';
17-
import 'package:lottie/lottie.dart';
13+
import 'package:flutter/widgets.dart';
14+
import 'package:flutter_svg/flutter_svg.dart' as _svg;
15+
import 'package:lottie/lottie.dart' as _lottie;
16+
import 'package:rive/rive.dart' as _rive;
17+
import 'package:vector_graphics/vector_graphics.dart' as _vg;
1818

1919
class $AssetsFlareGen {
2020
const $AssetsFlareGen();
@@ -316,7 +316,7 @@ class SvgGenImage {
316316
final Set<String> flavors;
317317
final bool _isVecFormat;
318318

319-
SvgPicture svg({
319+
_svg.SvgPicture svg({
320320
Key? key,
321321
bool matchTextDirection = false,
322322
AssetBundle? bundle,
@@ -329,29 +329,29 @@ class SvgGenImage {
329329
WidgetBuilder? placeholderBuilder,
330330
String? semanticsLabel,
331331
bool excludeFromSemantics = false,
332-
SvgTheme? theme,
332+
_svg.SvgTheme? theme,
333333
ColorFilter? colorFilter,
334334
Clip clipBehavior = Clip.hardEdge,
335335
@deprecated Color? color,
336336
@deprecated BlendMode colorBlendMode = BlendMode.srcIn,
337337
@deprecated bool cacheColorFilter = false,
338338
}) {
339-
final BytesLoader loader;
339+
final _svg.BytesLoader loader;
340340
if (_isVecFormat) {
341-
loader = AssetBytesLoader(
341+
loader = _vg.AssetBytesLoader(
342342
_assetName,
343343
assetBundle: bundle,
344344
packageName: package,
345345
);
346346
} else {
347-
loader = SvgAssetLoader(
347+
loader = _svg.SvgAssetLoader(
348348
_assetName,
349349
assetBundle: bundle,
350350
packageName: package,
351351
theme: theme,
352352
);
353353
}
354-
return SvgPicture(
354+
return _svg.SvgPicture(
355355
loader,
356356
key: key,
357357
matchTextDirection: matchTextDirection,
@@ -384,22 +384,22 @@ class FlareGenImage {
384384
final String _assetName;
385385
final Set<String> flavors;
386386

387-
FlareActor flare({
387+
_flare_actor.FlareActor flare({
388388
String? boundsNode,
389389
String? animation,
390390
BoxFit fit = BoxFit.contain,
391391
Alignment alignment = Alignment.center,
392392
bool isPaused = false,
393393
bool snapToEnd = false,
394-
FlareController? controller,
395-
FlareCompletedCallback? callback,
394+
_flare_controller.FlareController? controller,
395+
_flare_actor.FlareCompletedCallback? callback,
396396
Color? color,
397397
bool shouldClip = true,
398398
bool sizeFromArtboard = false,
399399
String? artboard,
400400
bool antialias = true,
401401
}) {
402-
return FlareActor(
402+
return _flare_actor.FlareActor(
403403
_assetName,
404404
boundsNode: boundsNode,
405405
animation: animation,
@@ -431,7 +431,7 @@ class RiveGenImage {
431431
final String _assetName;
432432
final Set<String> flavors;
433433

434-
RiveAnimation rive({
434+
_rive.RiveAnimation rive({
435435
String? artboard,
436436
List<String> animations = const [],
437437
List<String> stateMachines = const [],
@@ -440,10 +440,10 @@ class RiveGenImage {
440440
Widget? placeHolder,
441441
bool antialiasing = true,
442442
bool useArtboardSize = false,
443-
List<RiveAnimationController> controllers = const [],
444-
OnInitCallback? onInit,
443+
List<_rive.RiveAnimationController> controllers = const [],
444+
_rive.OnInitCallback? onInit,
445445
}) {
446-
return RiveAnimation.asset(
446+
return _rive.RiveAnimation.asset(
447447
_assetName,
448448
artboard: artboard,
449449
animations: animations,
@@ -472,19 +472,23 @@ class LottieGenImage {
472472
final String _assetName;
473473
final Set<String> flavors;
474474

475-
LottieBuilder lottie({
475+
_lottie.LottieBuilder lottie({
476476
Animation<double>? controller,
477477
bool? animate,
478-
FrameRate? frameRate,
478+
_lottie.FrameRate? frameRate,
479479
bool? repeat,
480480
bool? reverse,
481-
LottieDelegates? delegates,
482-
LottieOptions? options,
483-
void Function(LottieComposition)? onLoaded,
484-
LottieImageProviderFactory? imageProviderFactory,
481+
_lottie.LottieDelegates? delegates,
482+
_lottie.LottieOptions? options,
483+
void Function(_lottie.LottieComposition)? onLoaded,
484+
_lottie.LottieImageProviderFactory? imageProviderFactory,
485485
Key? key,
486486
AssetBundle? bundle,
487-
Widget Function(BuildContext, Widget, LottieComposition?)? frameBuilder,
487+
Widget Function(
488+
BuildContext,
489+
Widget,
490+
_lottie.LottieComposition?,
491+
)? frameBuilder,
488492
ImageErrorWidgetBuilder? errorBuilder,
489493
double? width,
490494
double? height,
@@ -495,7 +499,7 @@ class LottieGenImage {
495499
FilterQuality? filterQuality,
496500
void Function(String)? onWarning,
497501
}) {
498-
return Lottie.asset(
502+
return _lottie.Lottie.asset(
499503
_assetName,
500504
controller: controller,
501505
animate: animate,

examples/example_resources/lib/gen/assets.gen.dart

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
// ignore_for_file: type=lint
88
// ignore_for_file: directives_ordering,unnecessary_import,implicit_dynamic_list_literal,deprecated_member_use
99

10-
import 'package:flutter/widgets.dart';
10+
import 'package:flare_flutter/flare_actor.dart' as _flare_actor;
11+
import 'package:flare_flutter/flare_controller.dart' as _flare_controller;
1112
import 'package:flutter/services.dart';
12-
import 'package:flutter_svg/flutter_svg.dart';
13-
import 'package:vector_graphics/vector_graphics.dart';
14-
import 'package:flare_flutter/flare_actor.dart';
15-
import 'package:flare_flutter/flare_controller.dart';
16-
import 'package:rive/rive.dart';
17-
import 'package:lottie/lottie.dart';
13+
import 'package:flutter/widgets.dart';
14+
import 'package:flutter_svg/flutter_svg.dart' as _svg;
15+
import 'package:lottie/lottie.dart' as _lottie;
16+
import 'package:rive/rive.dart' as _rive;
17+
import 'package:vector_graphics/vector_graphics.dart' as _vg;
1818

1919
class $AssetsImagesGen {
2020
const $AssetsImagesGen();
@@ -167,7 +167,7 @@ class SvgGenImage {
167167

168168
static const String package = 'example_resources';
169169

170-
SvgPicture svg({
170+
_svg.SvgPicture svg({
171171
Key? key,
172172
bool matchTextDirection = false,
173173
AssetBundle? bundle,
@@ -181,29 +181,29 @@ class SvgGenImage {
181181
WidgetBuilder? placeholderBuilder,
182182
String? semanticsLabel,
183183
bool excludeFromSemantics = false,
184-
SvgTheme? theme,
184+
_svg.SvgTheme? theme,
185185
ColorFilter? colorFilter,
186186
Clip clipBehavior = Clip.hardEdge,
187187
@deprecated Color? color,
188188
@deprecated BlendMode colorBlendMode = BlendMode.srcIn,
189189
@deprecated bool cacheColorFilter = false,
190190
}) {
191-
final BytesLoader loader;
191+
final _svg.BytesLoader loader;
192192
if (_isVecFormat) {
193-
loader = AssetBytesLoader(
193+
loader = _vg.AssetBytesLoader(
194194
_assetName,
195195
assetBundle: bundle,
196196
packageName: package,
197197
);
198198
} else {
199-
loader = SvgAssetLoader(
199+
loader = _svg.SvgAssetLoader(
200200
_assetName,
201201
assetBundle: bundle,
202202
packageName: package,
203203
theme: theme,
204204
);
205205
}
206-
return SvgPicture(
206+
return _svg.SvgPicture(
207207
loader,
208208
key: key,
209209
matchTextDirection: matchTextDirection,
@@ -238,22 +238,22 @@ class FlareGenImage {
238238

239239
static const String package = 'example_resources';
240240

241-
FlareActor flare({
241+
_flare_actor.FlareActor flare({
242242
String? boundsNode,
243243
String? animation,
244244
BoxFit fit = BoxFit.contain,
245245
Alignment alignment = Alignment.center,
246246
bool isPaused = false,
247247
bool snapToEnd = false,
248-
FlareController? controller,
249-
FlareCompletedCallback? callback,
248+
_flare_controller.FlareController? controller,
249+
_flare_actor.FlareCompletedCallback? callback,
250250
Color? color,
251251
bool shouldClip = true,
252252
bool sizeFromArtboard = false,
253253
String? artboard,
254254
bool antialias = true,
255255
}) {
256-
return FlareActor(
256+
return _flare_actor.FlareActor(
257257
'packages/example_resources/$_assetName',
258258
boundsNode: boundsNode,
259259
animation: animation,
@@ -287,7 +287,7 @@ class RiveGenImage {
287287

288288
static const String package = 'example_resources';
289289

290-
RiveAnimation rive({
290+
_rive.RiveAnimation rive({
291291
String? artboard,
292292
List<String> animations = const [],
293293
List<String> stateMachines = const [],
@@ -296,10 +296,10 @@ class RiveGenImage {
296296
Widget? placeHolder,
297297
bool antialiasing = true,
298298
bool useArtboardSize = false,
299-
List<RiveAnimationController> controllers = const [],
300-
OnInitCallback? onInit,
299+
List<_rive.RiveAnimationController> controllers = const [],
300+
_rive.OnInitCallback? onInit,
301301
}) {
302-
return RiveAnimation.asset(
302+
return _rive.RiveAnimation.asset(
303303
'packages/example_resources/$_assetName',
304304
artboard: artboard,
305305
animations: animations,
@@ -330,19 +330,23 @@ class LottieGenImage {
330330

331331
static const String package = 'example_resources';
332332

333-
LottieBuilder lottie({
333+
_lottie.LottieBuilder lottie({
334334
Animation<double>? controller,
335335
bool? animate,
336-
FrameRate? frameRate,
336+
_lottie.FrameRate? frameRate,
337337
bool? repeat,
338338
bool? reverse,
339-
LottieDelegates? delegates,
340-
LottieOptions? options,
341-
void Function(LottieComposition)? onLoaded,
342-
LottieImageProviderFactory? imageProviderFactory,
339+
_lottie.LottieDelegates? delegates,
340+
_lottie.LottieOptions? options,
341+
void Function(_lottie.LottieComposition)? onLoaded,
342+
_lottie.LottieImageProviderFactory? imageProviderFactory,
343343
Key? key,
344344
AssetBundle? bundle,
345-
Widget Function(BuildContext, Widget, LottieComposition?)? frameBuilder,
345+
Widget Function(
346+
BuildContext,
347+
Widget,
348+
_lottie.LottieComposition?,
349+
)? frameBuilder,
346350
ImageErrorWidgetBuilder? errorBuilder,
347351
double? width,
348352
double? height,
@@ -354,7 +358,7 @@ class LottieGenImage {
354358
FilterQuality? filterQuality,
355359
void Function(String)? onWarning,
356360
}) {
357-
return Lottie.asset(
361+
return _lottie.Lottie.asset(
358362
_assetName,
359363
controller: controller,
360364
animate: animate,

packages/core/lib/generators/assets_generator.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ import 'dart:io';
33

44
import 'package:collection/collection.dart';
55
import 'package:dart_style/dart_style.dart';
6-
import 'package:dartx/dartx.dart';
6+
import 'package:dartx/dartx.dart' hide IterableSorted;
77
import 'package:flutter_gen_core/generators/generator_helper.dart';
88
import 'package:flutter_gen_core/generators/integrations/flare_integration.dart';
99
import 'package:flutter_gen_core/generators/integrations/image_integration.dart';
1010
import 'package:flutter_gen_core/generators/integrations/integration.dart';
1111
import 'package:flutter_gen_core/generators/integrations/lottie_integration.dart';
1212
import 'package:flutter_gen_core/generators/integrations/rive_integration.dart';
1313
import 'package:flutter_gen_core/generators/integrations/svg_integration.dart';
14-
import 'package:flutter_gen_core/settings/asset_type.dart';
1514
import 'package:flutter_gen_core/settings/config.dart';
1615
import 'package:flutter_gen_core/settings/flavored_asset.dart';
1716
import 'package:flutter_gen_core/settings/pubspec.dart';
@@ -56,12 +55,10 @@ String generateAssets(
5655
) {
5756
if (config.assets.isEmpty) {
5857
throw const InvalidSettingsException(
59-
'The value of "flutter/assets:" is incorrect.');
58+
'The value of "flutter/assets:" is incorrect.',
59+
);
6060
}
6161

62-
final importsBuffer = StringBuffer();
63-
final classesBuffer = StringBuffer();
64-
6562
final integrations = <Integration>[
6663
ImageIntegration(config.packageParameterLiteral,
6764
parseMetadata: config.flutterGen.parseMetadata),
@@ -158,6 +155,7 @@ String generateAssets(
158155
└────────────────────────────────────────────────────────────────────────────────────────┘''');
159156
}
160157

158+
final classesBuffer = StringBuffer();
161159
if (config.flutterGen.assets.outputs.isDotDelimiterStyle) {
162160
classesBuffer.writeln(_dotDelimiterStyleDefinition(config, integrations));
163161
} else if (config.flutterGen.assets.outputs.isSnakeCaseStyle) {
@@ -168,19 +166,18 @@ String generateAssets(
168166
throw 'The value of "flutter_gen/assets/style." is incorrect.';
169167
}
170168

171-
final imports = <String>{};
172-
integrations
173-
.where((integration) => integration.isEnabled)
174-
.forEach((integration) {
169+
final imports = <Import>{};
170+
for (final integration in integrations.where((e) => e.isEnabled)) {
175171
imports.addAll(integration.requiredImports);
176172
classesBuffer.writeln(integration.classOutput);
177-
});
178-
for (final package in imports) {
179-
importsBuffer.writeln(import(package));
180173
}
181174

182-
final buffer = StringBuffer();
175+
final importsBuffer = StringBuffer();
176+
for (final e in imports.sorted((a, b) => a.import.compareTo(b.import))) {
177+
importsBuffer.writeln(import(e));
178+
}
183179

180+
final buffer = StringBuffer();
184181
buffer.writeln(header);
185182
buffer.writeln(ignore);
186183
buffer.writeln(importsBuffer.toString());

packages/core/lib/generators/generator_helper.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'package:flutter_gen_core/settings/import.dart';
2+
13
String get header {
24
return '''/// GENERATED CODE - DO NOT MODIFY BY HAND
35
/// *****************************************************
@@ -15,4 +17,7 @@ String get ignore {
1517
''';
1618
}
1719

18-
String import(String package) => 'import \'$package\';';
20+
String import(Import package) {
21+
return 'import \'${package.import}\''
22+
'${package.alias != null ? ' as ${package.alias}' : ''};';
23+
}

0 commit comments

Comments
 (0)