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

dynamic_themes 封面
插件详情v1.1.0

dynamic_themes

允许您指定一组颜色主题供用户选择。在应用重启后保留所选主题。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2710
喜欢数
43
Pub 分数
150

安装插件

flutter pub add dynamic_themes

插件介绍

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

Dynamic Themes 是一个 Flutter 包,允许开发者实现可自定义的颜色主题,使用户能够选择并切换多个预设的主题。它会自动在应用重启后保留用户的主题选择,确保体验的一致性。该包支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,非常适合需要动态主题功能的跨平台应用程序。

适合场景

  • 带有用户可选浅色和深色模式的应用程序
  • 单个应用程序中的多主题支持
  • 应用重启后保持主题偏好
  • 跨平台主题一致性
  • 自定义品牌色彩方案
  • 用户偏好保留

主要特点

  • 轻松的主题配置
  • 自动主题持久化
  • 支持所有 Flutter 平台
  • 主题切换的简单 API
  • 内置的浅色/深色模式预设
  • 可扩展的主题系统

适合对象

  • 开发具有可自定义 UI 主题的应用程序的开发者
  • 针对多个平台的应用程序
  • 需要持久化用户偏好设置的项目
  • 使用品牌色系设计系统
  • 专注于用户体验和可访问性的应用程序

使用建议

  • 需要手动设置主题颜色
  • 仅限预定义的主题集
  • 无运行时主题生成
  • 主题切换可能需要重新构建
  • 依赖共享偏好进行持久化
  • 未针对非常大的主题集合进行优化

原始 README

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

dynamic_themes

"Buy Me A Coffee"

Dynamically change between multiple themes

This package allows you to specify multiple themes for the user to choose from. The theme can be updated dynamically during runtime. The package persits the selected theme across app restarts.

image

Getting Started

Follow the instructions here.

Usage

This package allows for an arbitrary number of themes to be defined. Each theme has a unique ID of type int. To create the themes and map them to IDs, first you have to create an instance of ThemeCollection, e. g.:

final themeCollection = ThemeCollection(
    themes: {
        0: ThemeData(primarySwatch: Colors.blue),
        1: ThemeData(primarySwatch: Colors.red),
        2: ThemeData.dark(),
    },
    fallbackTheme: ThemeData.light(), // optional fallback theme, default value is ThemeData.light()
);

However, I would recommend to introduce a class with static const int values to associate a name to each ID value:


class AppThemes {
    static const int LightBlue = 0;
    static const int LightRed = 1;
    static const int Dark = 2;
}
final themeCollection = ThemeCollection(
    themes: {
        AppThemes.LightBlue: ThemeData(primarySwatch: Colors.blue),
        AppThemes.LightRed: ThemeData(primarySwatch: Colors.red),
        AppThemes.Dark: ThemeData.dark(),
    },
    fallbackTheme: ThemeData.light(),
);

Now, wrap your material app like this:

return DynamicTheme(
    themeCollection: themeCollection,
    defaultThemeId: AppThemes.LightBlue, // optional, default id is 0
    builder: (context, theme) {
        return MaterialApp(
            title: 'dynamic_themes example',
            theme: theme,
            home: HomePage(title: 'dynamic_themes example app'),
        );
    }
);

The theme can be set anywhere in the app, provided you have a BuildContext, e. g.:

DynamicTheme.of(context).setTheme(AppThemes.LightRed);

By setting the theme, its ID is automatically saved via the shared_preferences package, so the next time the app starts, the theme can be restored automatically.

Provided you have wrapped the MaterialApp in the DynamicTheme widget, you can access the current theme like you are used to it by calling

final theme = Theme.of(context);

If for any reason you can't access the theme this way, you can also call

final theme = DynamicTheme.of(context).theme;

To get the ID of the current theme, for example to create a selection UI as done in the example app provided with this package, call

final themeId = DynamicTheme.of(context).themeId;

Example

The example app can be found in the example folder. It implements a dropdown menu to select between 4 themes. The app is the source for the screenshots above.

Maintainer

If you experience any problems using this package, please create an issue on Github. Pull requests are also very welcome.

Many thanks to the dynamic_theme package from Norbert Kozsir this package was inspired by!

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。