theme_extensions_gen_annotations 0.1.2
theme_extensions_gen_annotations: ^0.1.2 copied to clipboard
Annotations for the theme_extensions_gen code generator. This package is intended to be used alongside the theme_extensions_gen package.
theme_extensions_gen_annotations #
This package defines annotations used with theme_extensions_gen to generate strongly-typed ThemeExtension classes and tooling for Flutter theming.
π― Purpose #
Use these annotations to eliminate boilerplate in theme creation. Combined with build_runner and theme_extensions_gen, this package enables:
- Declarative theme structure via
@ThemeExtensionTemplate - Centralized and typed theme values via
@ThemeExtensionImpl - Optional build-time generation of
BuildContextextensions
π Annotations #
@ThemeExtensionTemplate() #
Marks an abstract interface as a template for a ThemeExtension. The generator creates:
copyWith,lerp,==,hashCode, andruntimeTypedebugFillPropertiesif the interface usesDiagnosticable- A concrete class with a factory constructor and mixin
- A
BuildContextextension with getters for all extensions
Example:
@ThemeExtensionTemplate()
abstract interface class BrandedButtonTheme
extends ThemeExtension<BrandedButtonTheme>
with _$BrandedButtonThemeMixin, Diagnosticable {
const factory BrandedButtonTheme({
required Decoration decoration,
required TextStyle textStyle,
required EdgeInsets padding,
}) = _$BrandedButtonTheme;
}
By default, the generator creates generated/theme_extensions/context_extensions.dart.
This can be customized or disabled via build.yaml:
targets:
$default:
builders:
contextExtensionsGenerator:
enabled: false
Or with a custom path:
contextExtensionsGenerator:
options:
output_path: "lib/custom/context_extensions.dart"
@ThemeExtensionImpl({ String? group }) #
Marks a list of theme implementations to be combined into a generated list.
Used like this:
@ThemeExtensionImpl()
List<ThemeExtension> get someFeatureThemeExtensions => const [
BrandedCardTheme(
//
),
BrandedButtonTheme(/*...*/),
];
@ThemeExtensionImpl(group: 'dark')
List<ThemeExtension> get someFeatureThemeExtensions => const [
BrandedCardTheme(/*...*/),
BrandedButtonTheme(/*...*/),
];
The generator then combines all matching lists into:
-
A default file:
List<ThemeExtension> get themeExtensions => [ ...someFeatureThemeExtensions, ...someAnotherFeatureThemeExtensions, ]; -
A group-specific file (e.g. for
"dark"):List<ThemeExtension> get themeExtensionsDark => [ ...someFeatureThemeExtensions, ];
Configuration in build.yaml:
targets:
$default:
builders:
themeExtensionsImplCombiner:
options:
default_output:
path: "lib/generated/theme_extensions/theme_extensions.dart"
list_name: "themeExtensions"
groups:
dark:
path: "lib/generated/theme_extensions/theme_extensions_dark.dart"
list_name: "themeExtensionsDark"