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 ffe99173..13de1a94 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 @@ -1,9 +1,43 @@ import 'package:parabeac_core/generation/flutter_project_builder/flutter_project_builder.dart'; import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/global_styling/colors_post_gen_task.dart'; import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/global_styling/text_styles_post_gen_task.dart'; +import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/global_styling/theming_post_gen_task.dart'; import 'package:pbdl/pbdl.dart'; class GlobalStylingAggregator { + /// List of all supported TextStyles that should be exported + /// for the deeper theming integration + static const _textStyleList = [ + 'bodyLarge', + 'bodyMedium', + 'bodySmall', + 'bodyText1', + 'bodyText2', + 'button', + 'caption', + 'displayLarge', + 'displayMedium', + 'displaySmall', + 'headline1', + 'headline2', + 'headline3', + 'headline4', + 'headline5', + 'headline6', + 'headlineLarge', + 'headlineMedium', + 'headlineSmall', + 'labelLarge', + 'labelMedium', + 'labelSmall', + 'overline', + 'subtitle1', + 'subtitle2', + 'titleLarge', + 'titleMedium', + 'titleSmall', + ]; + /// Examines [globalStyles] and adds PostGenTasks to [builder] static void addPostGenTasks( FlutterProjectBuilder builder, PBDLGlobalStyles globalStyles) { @@ -24,5 +58,24 @@ class GlobalStylingAggregator { ), ); } + + var themeTextStyles = []; + + for (var style in globalStyles.textStyles) { + if (_textStyleList.contains(style.name)) { + /// Add Text Style to Global theming list + themeTextStyles.add(style); + } + } + + if (themeTextStyles.isNotEmpty) { + /// Add Theme Styles to the Theme task + builder.postGenTasks.add( + ThemingPostGenTask( + builder.generationConfiguration, + themeTextStyles, + ), + ); + } } } 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 new file mode 100644 index 00000000..56a0ceb3 --- /dev/null +++ b/lib/generation/flutter_project_builder/post_gen_tasks/global_styling/theming_post_gen_task.dart @@ -0,0 +1,66 @@ +import 'package:get_it/get_it.dart'; +import 'package:parabeac_core/controllers/main_info.dart'; +import 'package:parabeac_core/generation/flutter_project_builder/post_gen_tasks/post_gen_task.dart'; +import 'package:parabeac_core/generation/generators/value_objects/file_structure_strategy/commands/add_constant_command.dart'; +import 'package:parabeac_core/generation/generators/value_objects/file_structure_strategy/file_ownership_policy.dart'; +import 'package:parabeac_core/generation/generators/value_objects/file_structure_strategy/path_services/path_service.dart'; +import 'package:parabeac_core/generation/generators/value_objects/generation_configuration/pb_generation_configuration.dart'; +import 'package:pbdl/pbdl.dart'; +import 'package:recase/recase.dart'; +import 'package:uuid/uuid.dart'; + +class ThemingPostGenTask extends PostGenTask { + GenerationConfiguration generationConfiguration; + List textStyles; + + List colors; + + ThemingPostGenTask( + this.generationConfiguration, + this.textStyles, + ); + + @override + void execute() { + final projectName = MainInfo().projectName; + + /// Map the [TextStyle] attributes in the project for [TextTheme] + final textThemeAttributes = textStyles + .map((style) => + '${style.name}: ${projectName.pascalCase}TextStyles.${style.name},') + .join(); + final textThemeBoilerplate = 'TextTheme($textThemeAttributes)'; + + final textConstant = ConstantHolder( + 'TextTheme', + 'textTheme', + textThemeBoilerplate, + ); + + final themeConstant = ConstantHolder( + 'ThemeData', + 'themeData', + 'ThemeData(textTheme: textTheme,)', + isconst: false, + ); + + var pathService = GetIt.I.get(); + + /// 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'; +'''; + + generationConfiguration.fileStructureStrategy.commandCreated( + WriteConstantsCommand( + Uuid().v4(), + [themeConstant, textConstant], + filename: '${projectName.snakeCase}_theme', + ownershipPolicy: FileOwnership.PBC, + imports: imports, + relativePath: pathService.themingRelativePath, + ), + ); + } +} diff --git a/lib/generation/generators/value_objects/file_structure_strategy/commands/add_constant_command.dart b/lib/generation/generators/value_objects/file_structure_strategy/commands/add_constant_command.dart index 58019d53..3f5b7e22 100644 --- a/lib/generation/generators/value_objects/file_structure_strategy/commands/add_constant_command.dart +++ b/lib/generation/generators/value_objects/file_structure_strategy/commands/add_constant_command.dart @@ -54,7 +54,7 @@ class WriteConstantsCommand extends FileStructureCommand { var description = constant.description.isNotEmpty ? '/// ${constant.description}' : ''; var constStr = - 'static const ${constant.type} ${constant.name} = ${constant.value};'; + 'static ${constant.isconst ? 'const' : ''} ${constant.type} ${constant.name} = ${constant.value};'; constBuffer.writeln('$description\n$constStr'); }); @@ -87,10 +87,14 @@ class ConstantHolder { /// Optional description to put as comment above the constant String description; + /// Whether [this] should have "const" written in it. + bool isconst; //TODO: Temporary bool in order to write theming file. + ConstantHolder( this.type, this.name, this.value, { + this.isconst = true, this.description = '', }); } diff --git a/test/golden/golden_files/global_styling/styling_text_styles.golden b/test/golden/golden_files/global_styling/styling_text_styles.golden index 94dc67b3..4af97716 100644 --- a/test/golden/golden_files/global_styling/styling_text_styles.golden +++ b/test/golden/golden_files/global_styling/styling_text_styles.golden @@ -27,4 +27,34 @@ static const TextStyle newStyle = TextStyle( fontStyle: FontStyle.normal, ) ; +/// headline1 description +static const TextStyle headline1 = TextStyle( + fontSize: 12.0, + fontWeight: FontWeight.w400, + letterSpacing: 0.0, + fontFamily: 'Inter', + decoration: TextDecoration.none, + fontStyle: FontStyle.normal, +) +; + +static const TextStyle mynewStyle = TextStyle( + fontSize: 12.0, + fontWeight: FontWeight.w400, + letterSpacing: 0.0, + fontFamily: 'Poppins', + decoration: TextDecoration.none, + fontStyle: FontStyle.normal, +) +; + +static const TextStyle headline2 = TextStyle( + fontSize: 12.0, + fontWeight: FontWeight.w700, + letterSpacing: 6.0, + fontFamily: 'Inter', + decoration: TextDecoration.none, + fontStyle: FontStyle.italic, +) +; }