buildSlideData function
Implementation
Future<void> buildSlideData(List<SlideData> slides) async {
bool hasMixes = false;
final literalSlideList = literalList(slides.map((slide) {
final options = slide.options;
return refer('SlideData').newInstance([], {
'name': literalString(slide.name),
'content': literalString(RegExp.escape(slide.content ?? ""), raw: false),
'options': refer('SlideOptions').newInstance([], {
'devicePreview': literalBool(options.devicePreview),
'scrollable': literalBool(options.scrollable),
'layout': CodeExpression(Code(options.layout.toString())),
'image':
options.image == null ? literalNull : literalString(options.image!),
'imageFit': CodeExpression(
Code(options.imageFit.toString()),
),
'verticalAlignment': CodeExpression(
Code(options.verticalAlignment.toString()),
),
'horizontalAlignment': CodeExpression(
Code(options.horizontalAlignment.toString()),
),
'mix': options.mix == null ? literalNull : literalString(options.mix!),
'codePreview': literalBool(options.codePreview),
}),
});
}));
final variable = literalSlideList.assignFinal('slides').statement;
// Side widgets
final sideWidgets = <String, Expression>{};
final slideMixes = <String, Expression>{};
for (var slide in slides) {
for (var snippet in slide.snippets) {
// final widgetNames = sideWidgets[slide.name] ?? <Expression>[];
if (slide.options.codePreview) {
sideWidgets[slide.name] = refer(snippet.widgetName).newInstance([]);
}
}
if (slide.options.mix != null) {
// Trigger the import for mixes
hasMixes = true;
slideMixes[slide.name] = CodeExpression(
Code(slide.options.mix.toString()),
);
}
}
final literalWidgetReference = literalMap(sideWidgets);
final literalMixesReference = literalMap(slideMixes);
final sideWidgetVar =
literalWidgetReference.assignFinal('sideWidgets').statement;
final slideMixesVar =
literalMixesReference.assignFinal('slideMixes').statement;
final library = Library((b) {
final builder = b
..body.addAll([variable, sideWidgetVar, slideMixesVar])
..directives.addAll([
Directive.import('package:dash_deck/showtime.dart'),
Directive.import('./code_preview.g.dart')
]);
if (hasMixes) {
builder.directives.add(Directive.import('./styles.dart'));
}
});
final slidesFile = kShowtimeDirectory.slidesFile;
await runCodeEmitter(library, slidesFile);
}