Skip to main content

lumina_theme 0.1.0 copy "lumina_theme: ^0.1.0" to clipboard
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 #

  1. Annotate a source class (instance final fields with initializers). Add Light/Dark suffixes 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),
  );
}
  1. Generate code:
flutter pub run build_runner build --delete-conflicting-outputs
  1. 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 ThemeExtension subclass with light()/dark() factories, copyWith, and lerp.
  • A ThemeData helper (<ClassName>ThemeData) that injects the extension and preserves existing extensions.
  • A BuildContext extension exposing your values via context.<contextName>.

Field rules #

  • Only instance final fields are supported; each must have an initializer.
  • Use FooLight/FooDark suffixes for mode-specific values; use Foo with 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 upgrade to bump analyzer/build_runner.
  • Missing .g.dart: ensure the source file has a part '...g.dart'; and that you ran flutter pub run build_runner build --delete-conflicting-outputs.
0
likes
140
points
11
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Code generation for ThemeExtension, ThemeData helpers, and context accessors from a single annotated source.

Repository (GitHub)

License

MIT (license)

Dependencies

analyzer, build, flutter, meta, source_gen

More

Packages that depend on lumina_theme