搜索插件、代码仓与 APP 分类

GitHub 代码仓pro100andrey/theme_extensions_builder

theme_extensions_builder_annotation

主题扩展构建器

theme_extensions_builder_annotation 项目封面
星标
7
复刻
6
最近推送(UTC)
2026年9月6日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

theme_extensions_builder

pub package License: MIT

📖 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.

🎯 Quick Links

📦 Installation

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_runner

Or add to pubspec.yaml:

dependencies:
  theme_extensions_builder_annotation: ^7.5.0

dev_dependencies:
  build_runner: ^2.13.0
  theme_extensions_builder: ^7.5.0

🚀 Quick Start

Using @ThemeExtensions

For 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 _$AppTheme {
  const AppTheme({
    required this.primaryColor,
    required this.spacing,
  });

  final Color primaryColor;
  final double spacing;
}

Generate code:

dart run build_runner build

Use in your app:

Widget build(BuildContext context) {
  final theme = context.appTheme;
  
  return Container(
    padding: EdgeInsets.all(theme.spacing),
    color: theme.primaryColor,
  );
}
Using @ThemeGen

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);
}

📋 Choose Your Annotation

@ThemeExtensions (Recommended)

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 ThemeExtension implementation with copyWith and lerp
  • BuildContext extension for convenient access
  • Equality operators and hashCode
@ThemeGen

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:

  • copyWith and merge methods
  • Static lerp method
  • Equality operators and hashCode

📖 Example

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

🛠️ Development

The repository is a pub workspace: one flutter pub get at the root resolves every package, including the example app.

flutter pub get
scripts/prepare_push.sh     # format, analyze, build and test every package
scripts/update_goldens.sh   # regenerate the golden files after a generator change

The generator tests run on the Dart SDK alone. The Flutter classes the fixtures use are small stand-ins in packages/theme_extensions_builder/test/fixtures/flutter_stubs.dart, with the same lerp and merge signatures as the real ones. The example app is where the generated code meets the real framework, so CI regenerates and analyzes it on every push.

📄 License

MIT License - see the LICENSE file for details.

Andrii GitHub avatar
GitHub 作者

Andrii ↗

Kharkiv, Urkaine官方网站 ↗
语言DartCMakeShellC++SwiftCKotlinObjective-C

分类

此仓库发布的插件

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗