From 9faee172a12783e9f2bdf310288f14519676d6be Mon Sep 17 00:00:00 2001 From: Ivan <42812006+ivan-015@users.noreply.github.com> Date: Sat, 25 Jun 2022 13:39:32 -0600 Subject: [PATCH 1/3] Generate ThemeData with TextStyles --- .../global_styling_aggregator.dart | 53 +++++++++++++++++ .../global_styling/theming_post_gen_task.dart | 59 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 lib/generation/flutter_project_builder/post_gen_tasks/global_styling/theming_post_gen_task.dart 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..648f6a3b --- /dev/null +++ b/lib/generation/flutter_project_builder/post_gen_tasks/global_styling/theming_post_gen_task.dart @@ -0,0 +1,59 @@ +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, + ); + + 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(), + [textConstant], + filename: '${projectName.snakeCase}_theme', + ownershipPolicy: FileOwnership.PBC, + imports: imports, + relativePath: pathService.themingRelativePath, + ), + ); + } +} From 89934219edf40de58479fda7211577f12e9479fa Mon Sep 17 00:00:00 2001 From: Ivan <42812006+ivan-015@users.noreply.github.com> Date: Tue, 28 Jun 2022 17:39:52 -0600 Subject: [PATCH 2/3] Writing ThemeData as part of the theme file --- .../global_styling/theming_post_gen_task.dart | 9 ++++++++- .../commands/add_constant_command.dart | 6 +++++- 2 files changed, 13 insertions(+), 2 deletions(-) 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 648f6a3b..56a0ceb3 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 @@ -37,6 +37,13 @@ class ThemingPostGenTask extends PostGenTask { textThemeBoilerplate, ); + final themeConstant = ConstantHolder( + 'ThemeData', + 'themeData', + 'ThemeData(textTheme: textTheme,)', + isconst: false, + ); + var pathService = GetIt.I.get(); /// Imports for material and the [TextStyles]. @@ -48,7 +55,7 @@ import 'package:${projectName.snakeCase}/${pathService.themingRelativePath.repla generationConfiguration.fileStructureStrategy.commandCreated( WriteConstantsCommand( Uuid().v4(), - [textConstant], + [themeConstant, textConstant], filename: '${projectName.snakeCase}_theme', ownershipPolicy: FileOwnership.PBC, imports: imports, 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 = '', }); } From 25cbbc4303f7043c05118ca971c81e425792c352 Mon Sep 17 00:00:00 2001 From: Ivan <42812006+ivan-015@users.noreply.github.com> Date: Tue, 28 Jun 2022 17:49:45 -0600 Subject: [PATCH 3/3] Replace golden test Add new textstyles to the test --- .../global_styling/styling_text_styles.golden | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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, +) +; }