搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。