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

theme_mode_builder 封面
插件详情v2.0.0

theme_mode_builder

一个用于处理应用程序主题的Flutter包,同时也使用共享偏好在设备上保存主题

AndroidiOSmacOSWindowsLinux
近 30 天下载
64
喜欢数
24
Pub 分数
160

安装插件

flutter pub add theme_mode_builder

插件介绍

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

一个 Flutter 包,可简化应用程序中的主题管理,实现明暗模式之间的无缝切换。它与共享偏好设置集成,可在应用重启后保留用户的主题选择,确保一致的体验。专为希望轻松实现主题自定义而无需样板代码的开发者设计。该包支持 Android、iOS、macOS、Windows 和 Linux 等多个平台。

适合场景

  • 实现亮色和暗色模式切换
  • 保留用户主题偏好
  • 使用动态主题构建响应式 UI
  • 创建可访问的应用程序界面
  • 启用用户自定义外观设置

主要特点

  • 通过共享首选项实现自动主题持久化
  • 主题状态管理的简单 API
  • 跨平台兼容性
  • 支持浅色和深色模式
  • 与现有 Flutter 项目的轻松集成

适合对象

  • 开发具有主题自定义功能的应用程序的开发者
  • 需要持久用户偏好设置的应用程序
  • 针对多个平台的项目
  • 以用户界面为重点的应用程序
  • 希望主题设置尽可能简单的团队

使用建议

  • 需要 shared_preferences 依赖
  • 仅限浅色/深色模式
  • 可能需要额外的逻辑来处理复杂的主题变体
  • 不适用于高级主题系统
  • 依赖于应用程序启动时的正确初始化

原始 README

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

Theme Mode Builder 🚀

Pub Version GitHub GitHub Issues GitHub Pull Requests

I created this package to help standardize my apps way of theme handling.

Web Demo: https://theme-mode-builder.netlify.app/

Features:
  • 🚀 Cross platform: mobile, desktop, browser
  • ❤ Simple, powerful, intuitive API
  • 🛡 Null Safety
  • 🔋 Batteries included

📚 How to Use

Table of contents
  1. Video
  2. Guide
Video: (A bit outdated, for v0.0.3)

Guide:

First run the initialization which is asynchronous so you will need to change the return type of your main() method from void to Future<void>:

Future<void> main() async {
  /// ensure widgets are initialized
  WidgetsFlutterBinding.ensureInitialized();

  /// initialize theme mode builder
  await ThemeModeBuilderConfig.ensureInitialized();

  /// Runs the app :)
  runApp(MyCoolApp());
}

Then just wrap your MaterialApp widget with the ThemeModeBuilder and return the MaterialApp widget from the builder. The builder gives you two arguments, builder: (BuildContext context, ThemeMode themeMode) {}. Pass the themeMode to themeMode on your MaterialApp.

Now this code below explains better than I do 🙈🚀😂:

import "package:flutter/material.dart";
import "package:theme_mode_builder/theme_mode_builder.dart";

class MyCoolApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ThemeModeBuilder(
      builder: (BuildContext context, ThemeMode themeMode) {
        return MaterialApp(
          title: "My Cool App",

          /// here is the `themeMode` passed form the `builder`
          themeMode: themeMode,
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(
              brightness: Brightness.light,
              seedColor: Colors.red,
            ),

            /// put your light theme data here
          ),
          darkTheme: ThemeData(
            colorScheme: ColorScheme.fromSeed(
              brightness: Brightness.dark,
              seedColor: Colors.deepPurple,
            ),

            /// put your dark theme data here
          ),
          home: Home(),
        );
      },
    );
  }
}
If you want to change the themeMode you just call:
await ThemeModeBuilderConfig.toggleTheme();

And the theme will change instantly given you followed the steps above correctly 🎉.

Want more customization?

To change to dark mode:

await ThemeModeBuilderConfig.setDark();

To change to light mode:

await ThemeModeBuilderConfig.setLight();

To change to system mode:

await ThemeModeBuilderConfig.setSystem();
To get the theme mode you can run:
final ThemeMode themeMode = ThemeModeBuilderConfig.getThemeMode();
Finally, To check if the current theme is dark or not, you use this simple call:
final bool isDarkTheme = ThemeModeBuilderConfig.isDarkTheme();

所属分类

依赖

依赖清单 4 项

源码仓库

使用此插件的项目