Flutter主题生成插件extension_theme_generator的使用

Flutter主题生成插件extension_theme_generator的使用

Pub

语言版本

  • 简体中文
  • 俄语

Flutter扩展主题生成器是一个工具,用于在Flutter项目中生成ThemeExtension,简化自定义主题的管理。通过使用两个装饰器@TextStyleAnnotation()@ColorAnnotation(),您可以轻松创建符合需求的主题。

主要特性

  • 易于使用:只需在包含颜色或文本样式的类上添加@TextStyleAnnotation()@ColorAnnotation()
  • 自动生成功能:运行命令flutter pub run build_runner build以自动生成ThemeExtension所需的代码。
  • 灵活性:您可以创建任意数量的应用程序主题,而不仅仅是深色和浅色主题。

示例用法

详细的示例可以在/example文件夹中找到。

步骤 1:添加依赖项

dependencies:
  extension_theme_generator: ^1.0.0

dev_dependencies:
  build_runner: ^2.4.11

步骤 2:添加颜色和文本样式类

颜色示例:

@ColorAnnotation()
class DarkColors {
  static const Color primary = Color(0xFF000000);
  static const Color secondary = Color(0xFF111111);
  static const Color textColor = Color(0xFF111111);
  static const Color background = Color(0xFF015D60);
}

@ColorAnnotation()
class BlueColors {
  static const Color primary = Color(0xFF000000);
  static const Color secondary = Color(0xFF111111);
  static const Color textColor = Color(0xFF111111);
  static const Color background = Color(0xFFC4EDF3);
}

文本样式示例:

@TextStyleAnnotation()
class DarkTextStyle {
  static const TextStyle header = TextStyle(
    fontSize: 24,
    fontWeight: FontWeight.bold,
    color: Colors.white,
  );
  static const TextStyle body = TextStyle(
    fontSize: 16,
    fontWeight: FontWeight.normal,
    color: Colors.black,
  );
}

@TextStyleAnnotation()
class LightTextStyle {
  static const TextStyle header = TextStyle(
    fontSize: 24,
    fontWeight: FontWeight.bold,
    color: Colors.black,
  );
  static const TextStyle body = TextStyle(
    fontSize: 16,
    fontWeight: FontWeight.normal,
    color: Colors.black,
  );
}

您可以添加任意数量的主题,并不仅仅限于深色和浅色主题。

步骤 3:添加生成文件路径

例如,part 'theme.g.dart';

步骤 4:运行生成命令

flutter pub run build_runner build

步骤 5:更新 ThemeData

ThemeData createDarkTheme(){
  return ThemeData(
    extensions: const [
      $AppThemeColors.darkColors, // 生成的颜色类
      $AppThemeTextStyles.darkTextStyle, // 生成的文本样式类
    ],
  );
}

示例代码

import 'package:example/theme/blue_theme.dart';
import 'package:example/theme/build_context_ext.dart';
import 'package:example/theme/dark_theme.dart';
import 'package:example/theme/light_theme.dart';
import 'package:flutter/material.dart';

import 'theme/theme.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  [@override](/user/override)
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  ThemeData? themeData;

  [@override](/user/override)
  void initState() {
    themeData=createLightTheme();
    super.initState();
  }

  void selectTheme(ThemeData data){
    setState(() {
      themeData=data;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: themeData,
      home: HomePage(
        selectTheme: (data)=>selectTheme(data),
      ),
    );
  }
}

class HomePage extends StatefulWidget {
  final Function(ThemeData data) selectTheme;
  const HomePage({super.key, required this.selectTheme});

  [@override](/user/override)
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar: AppBar(
        title: Text('Demo Theme App'),
      ),
      backgroundColor: context.appColor.background,
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              'Select an application theme:',
              style: context.appText.header,
            ),
            ElevatedButton(
                onPressed: (){
                  widget.selectTheme(createLightTheme());
                },
                child: Text('Light Theme')
            ),
            ElevatedButton(
                onPressed: (){
                  widget.selectTheme(createDarkTheme());
                },
                child: Text('Dark Theme')
            ),
            ElevatedButton(
                onPressed: (){
                  widget.selectTheme(createBlueTheme());
                },
                child: Text('Blue Theme')
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


当然,以下是如何在Flutter项目中使用extension_theme_generator插件来生成和应用主题的一个示例。假设你已经将extension_theme_generator插件添加到了你的pubspec.yaml文件中:

dependencies:
  flutter:
    sdk: flutter
  extension_theme_generator: ^最新版本号  # 请替换为实际版本号

1. 添加依赖并导入

首先,确保你已经运行了flutter pub get来安装依赖。然后,在你的Dart文件中导入插件:

import 'package:extension_theme_generator/extension_theme_generator.dart';

2. 定义主题数据

你可以通过定义一个类来存储你的主题数据。例如:

class MyThemeData {
  final Color primaryColor;
  final Color secondaryColor;
  final Color backgroundColor;
  final Color textColor;

  MyThemeData({
    required this.primaryColor,
    required this.secondaryColor,
    required this.backgroundColor,
    required this.textColor,
  });

  // 将主题数据转换为Material主题
  ThemeData toThemeData() {
    return ThemeData(
      primaryColor: primaryColor,
      primaryVariant: secondaryColor.withOpacity(0.7),
      secondaryColor: secondaryColor,
      backgroundColor: backgroundColor,
      textTheme: TextTheme(
        bodyText1: TextStyle(color: textColor),
      ),
    );
  }
}

3. 使用extension_theme_generator生成主题

接下来,使用extension_theme_generator来生成你的主题。你可以在一个单独的文件中配置你的主题生成逻辑,例如theme_generator.dart

import 'package:extension_theme_generator/extension_theme_generator.dart';
import 'package:flutter/material.dart';
import 'my_theme_data.dart';  // 假设MyThemeData类定义在这个文件中

class MyTheme extends StatelessWidget implements ThemeGeneratorWidget {
  final MyThemeData themeData;

  MyTheme({required this.themeData});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: themeData.toThemeData(),
      // 你的其他应用配置
      home: Scaffold(
        appBar: AppBar(
          title: Text('My Flutter App'),
        ),
        body: Center(
          child: Text('Hello, World!'),
        ),
      ),
    );
  }

  @override
  ThemeData generateTheme() {
    // 你可以在这里动态生成主题数据,这里我们直接使用传入的themeData
    return themeData.toThemeData();
  }
}

4. 在主入口文件中使用

最后,在你的主入口文件(通常是main.dart)中使用你生成的主题:

import 'package:flutter/material.dart';
import 'theme_generator.dart';  // 假设MyTheme类定义在这个文件中
import 'my_theme_data.dart';  // 假设MyThemeData类定义在这个文件中

void main() {
  // 你可以在这里动态设置你的主题数据
  final MyThemeData myThemeData = MyThemeData(
    primaryColor: Colors.blue,
    secondaryColor: Colors.green,
    backgroundColor: Colors.grey[200]!,
    textColor: Colors.black,
  );

  runApp(MyTheme(themeData: myThemeData));
}

注意事项

  • extension_theme_generator插件的具体用法和功能可能会随着版本更新而变化,请参考其官方文档获取最新信息。
  • 在实际项目中,你可能会希望从配置文件(如JSON文件)或用户输入中动态生成主题数据。
  • 上述示例代码是一个简单的演示,你可能需要根据实际需求对主题数据进行更复杂的配置。

希望这个示例能帮助你在Flutter项目中使用extension_theme_generator插件来生成和应用主题。

回到顶部