diff --git a/lib/generation/flutter_project_builder/post_gen_tasks/global_styling/global_styling_aggregator.dart b/lib/generation/flutter_project_builder/post_gen_tasks/global_styling/global_styling_aggregator.dart index ae118976..7b425f0a 100644 --- a/lib/generation/flutter_project_builder/post_gen_tasks/global_styling/global_styling_aggregator.dart +++ b/lib/generation/flutter_project_builder/post_gen_tasks/global_styling/global_styling_aggregator.dart @@ -42,39 +42,44 @@ class GlobalStylingAggregator { /// Examines [globalStyles] and adds PostGenTasks to [builder] static void addPostGenTasks( FlutterProjectBuilder builder, PBDLGlobalStyles globalStyles) { - if (globalStyles.colors != null && globalStyles.colors.isNotEmpty) { + /// Check whether there are theme or global colors + if ((globalStyles.colors != null && globalStyles.colors.isNotEmpty) || + (globalStyles.themeColors != null && + globalStyles.themeColors.isNotEmpty)) { + /// Aggregate all colors + final globalColors = globalStyles.colors + ..addAll(globalStyles.themeColors); builder.postGenTasks.add( ColorsPostGenTask( builder.generationConfiguration, - globalStyles.colors, + globalColors, ), ); } - if (globalStyles.textStyles != null && globalStyles.textStyles.isNotEmpty) { + /// Check whether there are theme or global textstyles + if ((globalStyles.textStyles != null && + globalStyles.textStyles.isNotEmpty) || + (globalStyles.themeTextStyles != null && + globalStyles.themeTextStyles.isNotEmpty)) { + final globalTextStyles = globalStyles.textStyles + ..addAll(globalStyles.themeTextStyles); builder.postGenTasks.add( TextStylesPostGenTask( builder.generationConfiguration, - globalStyles.textStyles, + globalTextStyles, ), ); } - var themeTextStyles = []; - - for (var style in globalStyles.textStyles) { - if (_textStyleList.contains(style.name.camelCase)) { - /// Add Text Style to Global theming list - themeTextStyles.add(style); - } - } - - if (themeTextStyles.isNotEmpty) { + if (globalStyles.themeTextStyles.isNotEmpty || + globalStyles.themeColors.isNotEmpty) { /// Add Theme Styles to the Theme task builder.postGenTasks.add( ThemingPostGenTask( builder.generationConfiguration, - themeTextStyles, + globalStyles.themeTextStyles ?? [], + globalStyles.themeColors ?? [], ), ); } diff --git a/lib/generation/flutter_project_builder/post_gen_tasks/global_styling/theming_post_gen_task.dart b/lib/generation/flutter_project_builder/post_gen_tasks/global_styling/theming_post_gen_task.dart index dc60cdcd..6c070d15 100644 --- a/lib/generation/flutter_project_builder/post_gen_tasks/global_styling/theming_post_gen_task.dart +++ b/lib/generation/flutter_project_builder/post_gen_tasks/global_styling/theming_post_gen_task.dart @@ -18,47 +18,94 @@ class ThemingPostGenTask extends PostGenTask { ThemingPostGenTask( this.generationConfiguration, this.textStyles, + this.colors, ); @override void execute() { - final projectName = MainInfo().projectName; + var pathService = GetIt.I.get(); - /// Map the [TextStyle] attributes in the project for [TextTheme] - final textThemeAttributes = textStyles - .map((style) => - '${style.name.camelCase}: ${projectName.pascalCase}TextStyles.${style.name.camelCase},') - .join(); - final textThemeBoilerplate = 'TextTheme($textThemeAttributes)'; + /// Relative path to theming folder for imports + final themingRelativePath = + pathService.themingRelativePath.replaceFirst('lib/', ''); - final textConstant = ConstantHolder( - 'TextTheme', - 'textTheme', - textThemeBoilerplate, - ); + final projectName = MainInfo().projectName; - final themeConstant = ConstantHolder( - 'ThemeData', - 'themeData', - 'ThemeData(textTheme: textTheme,)', - isconst: false, + final constants = []; + final importBuffer = StringBuffer( + 'import \'package:flutter/material.dart\';', ); - var pathService = GetIt.I.get(); + if (textStyles.isNotEmpty) { + importBuffer.writeln( + 'import \'package:${projectName.snakeCase}/$themingRelativePath/${projectName.snakeCase}_text_styles.g.dart\';', + ); + + /// Map the [TextStyle] attributes in the project for [TextTheme] + final textThemeAttributes = textStyles + .map((style) => + '${style.name.camelCase}: ${projectName.pascalCase}TextStyles.${style.name.camelCase},') + .join(); + final textThemeBoilerplate = 'TextTheme($textThemeAttributes)'; + + constants.add(ConstantHolder( + 'TextTheme', + 'textTheme', + textThemeBoilerplate, + )); + + /// Only create a [ThemeData] here if there are no [ColorSchemes]. + /// + /// This is because a [ThemeData] will already be generated for each [ColorSchem] + if (colors.isEmpty) { + constants.add(ConstantHolder( + 'ThemeData', + 'themeData', + 'ThemeData(textTheme: textTheme,)', + isconst: false, + )); + } + } + + if (colors.isNotEmpty) { + importBuffer.writeln( + 'import \'package:${projectName.snakeCase}/$themingRelativePath/${projectName.snakeCase}_colors.g.dart\';', + ); + + /// Map the [ColorSchemes] by name + final colorSchemeMap = >{}; + for (final color in colors) { + final attributeName = color.name.split('/').last; + final colorAttribute = + '$attributeName: ${projectName.pascalCase}Colors.${color.name.camelCase}'; + if (colorSchemeMap.containsKey(color.colorScheme)) { + colorSchemeMap[color.colorScheme].add(colorAttribute); + } else { + colorSchemeMap[color.colorScheme] = [colorAttribute]; + } + } - /// Imports for material and the [TextStyles]. - var imports = ''' -import 'package:flutter/material.dart'; -import 'package:${projectName.snakeCase}/${pathService.themingRelativePath.replaceFirst('lib/', '')}/${projectName.snakeCase}_text_styles.g.dart'; -'''; + /// Create the constants for [ThemeData] and [ColorSchemes] + for (final entry in colorSchemeMap.entries) { + final attributes = entry.value.join(','); + constants.add(ConstantHolder( + 'ColorScheme', entry.key, 'ColorScheme.${entry.key}($attributes)')); + constants.add(ConstantHolder( + 'ThemeData', + 'themeData${entry.key.pascalCase}', + 'ThemeData(${textStyles.isNotEmpty ? 'textTheme: textTheme,' : ''} colorScheme: ${entry.key},)', + isconst: false, + )); + } + } generationConfiguration.fileStructureStrategy.commandCreated( WriteConstantsCommand( Uuid().v4(), - [themeConstant, textConstant], + constants, filename: '${projectName.snakeCase}_theme', ownershipPolicy: FileOwnership.PBC, - imports: imports, + imports: importBuffer.toString(), relativePath: pathService.themingRelativePath, ), ); diff --git a/pubspec.yaml b/pubspec.yaml index 9bec921d..b1ab2247 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: parabeac_core description: Continuous Design / Continuous Integration for Figma-to-Flutter -version: 3.2.0 +version: 3.3.0 # homepage: https://www.example.com environment: