lumina_theme 0.1.0
lumina_theme: ^0.1.0 copied to clipboard
Code generation for ThemeExtension, ThemeData helpers, and context accessors from a single annotated source.
LuminaTheme #
LuminaTheme generates ThemeExtension classes, ThemeData helpers, and BuildContext accessors from a single annotated Dart class. Describe your light/dark values once, run build_runner, and get strongly typed theme APIs.
Install #
Add to your dev dependencies:
dev_dependencies:
lumina_theme: ^0.1.0
build_runner: ^2.4.7
Usage #
- Annotate a source class (instance
finalfields with initializers). AddLight/Darksuffixes for per-mode values or omit the suffix for a shared value.
import 'package:lumina_theme/lumina_theme.dart';
import 'package:flutter/material.dart';
part 'my_app_theme.g.dart';
@LuminaThemeSource(contextName: 'brand')
class MyAppTheme {
final Color primaryLight = const Color(0xFF123456);
final Color primaryDark = const Color(0xFF0F2345);
final Color warning = const Color(0xFFFFC107);
final double cardRadius = 12.0;
final TextStyle headingLight = const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
color: Color(0xFF132733),
);
final TextStyle headingDark = const TextStyle(
fontSize: 20,
fontWeight: FontWeight.w600,
color: Color(0xFFFAFAFA),
);
}
- Generate code:
flutter pub run build_runner build --delete-conflicting-outputs
- Use the generated APIs:
MaterialApp(
theme: MyAppThemeThemeData.light(),
darkTheme: MyAppThemeThemeData.dark(),
home: Builder(
builder: (context) {
final colors = context.brand;
return Container(
padding: colors.contentPadding,
decoration: BoxDecoration(
color: colors.primary,
borderRadius: BorderRadius.circular(colors.cardRadius),
),
child: Text('Hello', style: colors.heading),
);
},
),
);
What gets generated #
- A
ThemeExtensionsubclass withlight()/dark()factories,copyWith, andlerp. - A
ThemeDatahelper (<ClassName>ThemeData) that injects the extension and preserves existing extensions. - A
BuildContextextension exposing your values viacontext.<contextName>.
Field rules #
- Only instance
finalfields are supported; each must have an initializer. - Use
FooLight/FooDarksuffixes for mode-specific values; useFoowith no suffix for shared values. - Supported types include Color, TextStyle, EdgeInsets(Geometry), BorderRadius(Geometry), Radius, Size, Alignment(Geometry), BoxConstraints, ShapeBorder/OutlinedBorder/InputBorder, and numeric types (double/int/num). Others fall back to step interpolation.
Example #
See example/lib/my_app_theme.dart and the generated example/lib/my_app_theme.g.dart for a full sample.
Troubleshooting #
- Analyzer version warning: the generator works on Flutter 3.22+; if you see version warnings, run
flutter pub upgradeto bumpanalyzer/build_runner. - Missing
.g.dart: ensure the source file has apart '...g.dart';and that you ranflutter pub run build_runner build --delete-conflicting-outputs.