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

插件详情v7.5.0

theme_extensions_builder_annotation

主题扩展构建器的注释。此包在没有 theme_extensions_builder 的情况下什么也不做。

AndroidiOSLinuxmacOSWebWindows
近 30 天下载
19.3万
喜欢数
4
Pub 分数
160

安装插件

flutter pub add theme_extensions_builder_annotation

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Dart 包,为 theme_extensions_builder 提供注解,可简化 Flutter 应用中主题扩展的创建和管理。它是 theme_extensions_builder 的配套工具,通过注解提供编译时代码生成支持。该包本身不包含可执行逻辑,但通过简化主题配置并减少样板代码来提升开发效率。它仅设计用于与 theme_extensions_builder 包配合使用。

适合场景

  • 生成主题扩展类
  • 简化主题配置
  • 启用类型安全的主题处理
  • 减少主题设置中的样板代码
  • 支持动态主题工作流

主要特点

  • 编译时代码生成
  • 与 theme_extensions_builder 的集成
  • 类型安全的主题定义
  • 最小的运行时开销
  • 基于注解的开发

适合对象

  • Flutter 开发者使用 theme_extensions_builder
  • 注重可维护主题系统的团队
  • 需要可扩展主题解决方案的项目
  • 寻求减少样板代码的开发者
  • 具有复杂或动态主题需求的应用

使用建议

  • 需要 theme_extensions_builder 才能正常运行
  • 无独立功能
  • 依赖 build_runner 进行代码生成
  • 仅限在支持的 Flutter 项目中使用
  • 不适用于简单的静态主题

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

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.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

所在榜单