Flutter主题代码生成插件theme_code_gen的使用

Flutter主题代码生成插件theme_code_gen的使用

特性

使用熟悉的flutter pub run build_runner build命令和注解,您可以生成自己的代码以创建适当的ThemeExtension。

入门指南

在您的pubspec.yaml文件中添加以下包:

  • build_runner
  • theme_code_gen

创建一个类,包含自定义的颜色或文本样式(所有字段应为“静态”),并使用相应的注解标记该类(对于颜色 - @color_annotation,对于文本 - @text_annotation)。

不要忘记添加以下行:

part '-your file name-.g.dart';

运行以下命令:

flutter pub run build_runner build

您将获得已准备好的生成文件。生成文件后,必须移除所有依赖项(theme_code_gen)。每种颜色必须有“Dark”和“Light”的实现。确保在每个颜色名称的末尾使用“Light”或“Dark”。

使用示例

// 导入必要的包
import 'package:flutter/material.dart';

// 定义一个用于文本样式的类,并用 @text_annotation 注解标记
@text_annotation
class ConstantText {
  // 静态文本样式
  static TextStyle text1 = const TextStyle(fontSize: 33);
  static TextStyle text2 = const TextStyle(fontSize: 23);
}

// 定义一个用于颜色的类,并用 @color_annotation 注解标记
@color_annotation
class ConstantColor {
  // 静态颜色常量
  static const bgLight = Color(0xFF489D91); // Light 版本
  static const bgDark = Color(0xFF489D91);  // Dark 版本
}

生成文件示例

在执行flutter pub run build_runner build之后,会生成一个对应的.g.dart文件,例如constant_color.g.dart。此文件包含了生成的ThemeExtension代码。

// constant_color.g.dart

import 'constant_color.dart';

class GeneratedThemeLight extends ThemeExtension<GeneratedThemeLight> {
  final TextStyle text1 = ConstantText.text1;
  final TextStyle text2 = ConstantText.text2;
  final Color bgLight = ConstantColor.bgLight;

  [@override](/user/override)
  ThemeExtension<GeneratedThemeLight> copyWith() {
    return GeneratedThemeLight()
      ..text1 = text1
      ..text2 = text2
      ..bgLight = bgLight;
  }

  [@override](/user/override)
  ThemeExtension<GeneratedThemeLight> lerp(
      ThemeExtension<GeneratedThemeLight>? other, double t) {
    if (other is! GeneratedThemeLight) {
      return this;
    }
    return GeneratedThemeLight()
      ..text1 = TextStyle.lerp(text1, other.text1, t)
      ..text2 = TextStyle.lerp(text2, other.text2, t)
      ..bgLight = Color.lerp(bgLight, other.bgLight, t);
  }
}

class GeneratedThemeDark extends ThemeExtension<GeneratedThemeDark> {
  final TextStyle text1 = ConstantText.text1;
  final TextStyle text2 = ConstantText.text2;
  final Color bgDark = ConstantColor.bgDark;

  [@override](/user/override)
  ThemeExtension<GeneratedThemeDark> copyWith() {
    return GeneratedThemeDark()
      ..text1 = text1
      ..text2 = text2
      ..bgDark = bgDark;
  }

  [@override](/user/override)
  ThemeExtension<GeneratedThemeDark> lerp(
      ThemeExtension<GeneratedThemeDark>? other, double t) {
    if (other is! GeneratedThemeDark) {
      return this;
    }
    return GeneratedThemeDark()
      ..text1 = TextStyle.lerp(text1, other.text1, t)
      ..text2 = TextStyle.lerp(text2, other.text2, t)
      ..bgDark = Color.lerp(bgDark, other.bgDark, t);
  }
}

更多关于Flutter主题代码生成插件theme_code_gen的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter主题代码生成插件theme_code_gen的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


theme_code_gen 是一个用于 Flutter 的代码生成插件,它可以帮助开发者自动生成主题相关的代码,减少手动编写重复代码的工作量。通过使用 theme_code_gen,你可以更轻松地管理和维护 Flutter 应用中的主题样式。

以下是如何使用 theme_code_gen 插件的步骤:

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 theme_code_genbuild_runner 依赖。

dependencies:
  flutter:
    sdk: flutter
  theme_code_gen: ^latest_version

dev_dependencies:
  build_runner: ^latest_version

然后运行 flutter pub get 来获取依赖。

2. 创建主题配置类

接下来,你需要创建一个 Dart 类来定义你的主题配置。这个类将使用 theme_code_gen 提供的注解来标记主题属性。

import 'package:theme_code_gen/theme_code_gen.dart';

part 'app_theme.g.dart';

@ThemeDataGen()
class AppTheme {
  final Color primaryColor;
  final Color secondaryColor;
  final TextStyle headline1;
  final TextStyle bodyText1;

  AppTheme({
    required this.primaryColor,
    required this.secondaryColor,
    required this.headline1,
    required this.bodyText1,
  });
}

3. 生成代码

在定义好主题配置类之后,你可以使用 build_runner 来生成主题相关的代码。

在终端中运行以下命令:

flutter pub run build_runner build

这将会生成一个名为 app_theme.g.dart 的文件,其中包含了自动生成的代码。

4. 使用生成的代码

生成的代码将包含一个 ThemeData 的扩展方法,你可以直接在 Flutter 应用中使用它来应用主题。

import 'package:flutter/material.dart';
import 'app_theme.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appTheme = AppTheme(
      primaryColor: Colors.blue,
      secondaryColor: Colors.green,
      headline1: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
      bodyText1: TextStyle(fontSize: 16, color: Colors.black),
    );

    return MaterialApp(
      theme: appTheme.toThemeData(),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Theme Code Gen Example'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Headline 1',
              style: Theme.of(context).textTheme.headline1,
            ),
            Text(
              'Body Text 1',
              style: Theme.of(context).textTheme.bodyText1,
            ),
          ],
        ),
      ),
    );
  }
}

5. 自定义生成代码

你可以通过 @ThemeDataGen 注解中的参数来自定义生成的代码。例如,你可以指定生成的 ThemeData 的名称,或者是否生成某些特定的属性。

@ThemeDataGen(
  themeDataName: 'CustomThemeData',
  generateTextTheme: false,
)
class AppTheme {
  // 主题属性
}

6. 持续集成

如果你希望在每次代码更改时自动重新生成代码,可以使用 build_runnerwatch 模式:

flutter pub run build_runner watch
回到顶部