📖 Full documentation: packages/theme_extensions_builder/README.md
theme_extensions_builder is a powerful code generator for Flutter's ThemeExtension classes. It eliminates boilerplate code by automatically generating copyWith, lerp, ==, and hashCode methods, along with convenient BuildContext extensions for easy theme access.
Add the packages to your project:
flutter pub add theme_extensions_builder_annotation
flutter pub add --dev theme_extensions_builder
flutter pub add --dev build_runnerOr add to pubspec.yaml:
dependencies:
theme_extensions_builder_annotation: ^7.0.0
dev_dependencies:
build_runner: ^2.10.4
theme_extensions_builder: ^7.0.0For classes extending ThemeExtension:
import 'package:flutter/material.dart';
import 'package:theme_extensions_builder_annotation/theme_extensions_builder_annotation.dart';
part 'app_theme.g.theme.dart';
@ThemeExtensions()
class AppTheme extends ThemeExtension<AppTheme> with _$AppThemeMixin {
const AppTheme({
required this.primaryColor,
required this.spacing,
});
final Color primaryColor;
final double spacing;
}Generate code:
dart run build_runner buildUse in your app:
Widget build(BuildContext context) {
final theme = context.appTheme;
return Container(
padding: EdgeInsets.all(theme.spacing),
color: theme.primaryColor,
);
}For standalone theme data classes:
@ThemeGen()
class ButtonThemeData with _$ButtonThemeData {
const ButtonThemeData({
required this.backgroundColor,
this.borderRadius = 8.0,
});
final Color backgroundColor;
final double borderRadius;
static ButtonThemeData? lerp(ButtonThemeData? a, ButtonThemeData? b, double t) =>
_$ButtonThemeData.lerp(a, b, t);
}For classes extending Flutter's ThemeExtension - ideal for most use cases.
Perfect for:
- Full Flutter theme integration
- Automatic theme switching with lerp animations
- Easy access via BuildContext (e.g.,
context.appTheme)
Generates:
- Complete
ThemeExtensionimplementation withcopyWithandlerp - BuildContext extension for convenient access
- Equality operators and hashCode
For standalone theme data classes without ThemeExtension inheritance.
Perfect for:
- Custom theme systems outside standard Flutter themes
- Maximum control over class structure
- Nested theme data objects
Generates:
copyWithandmergemethods- Static
lerpmethod - Equality operators and hashCode
Check out the example project for a comprehensive Flutter app with:
- 5 Theme Extensions: App, Button, Card, Typography, and Spacing themes
- Theme Switching: Seamless light/dark mode transitions
- Custom Components: Buttons, cards, and typography showcases
- Best Practices: Real-world organization patterns
MIT License - see the LICENSE file for details.