搜尋套件、儲存庫與 APP 分類

dynamic_color_theme 封面
套件詳情v2.0.0

dynamic_color_theme

動態變更您的色彩主題,包含淺色和深色模式。在重新啟動後持續保留顏色。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
205
喜歡數
29
Pub 分數
140

安裝套件

flutter pub add dynamic_color_theme

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個 Flutter 套件,可實現亮色與暗色模式之間的動態主題切換,並在應用程式重新啟動後持續保留顏色偏好。此套件透過自動適應系統偏好或允許使用者自訂自定主題,簡化主題管理。該套件支援所有主要平台,確保在 Android、iOS、Web、macOS、Windows 及 Linux 上提供一致的 UI 經驗。開發者可輕鬆整合與自訂色彩方案,即使應用程式關閉並重新開啟後仍能維持狀態。

適用情境

  • 動態亮色/暗色模式切換
  • 保留使用者主題偏好
  • 自定義顏色主題實現
  • 跨平台主題一致性
  • 系統主題自動檢測
  • 應用中的主題自定義

主要特色

  • 支援淺色和深色模式
  • 持久化主題存儲
  • 可自訂的顏色方案
  • 自動系統主題檢測
  • 跨平台相容性
  • 與 MaterialApp 的輕鬆整合

適合對象

  • 需要主題持久化的應用
  • 開發跨平台應用的開發者
  • UI密集型應用
  • 具有使用者偏好設定的應用程式
  • 需要動態主題的設計系統

使用建議

  • 需要正確的狀態管理
  • 可能需要額外的邏輯來處理複雜的主题過渡
  • 僅限顏色主題(不包含完整UI元件)
  • 依賴於平台特定的儲存機制
  • 不適合高度複雜的主題工作流

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

dynamic_color_theme

This package allows you to dynamically change your color theme and will automatically switch between light and dark mode based on the new color. Additionally, the color values can be persisted across restarts by saving both the color and dark mode to shared preferences.

Installation

dependencies:
    dynamic_color_theme: ^2.0.0

Import the theme as shown below.

import 'package:dynamic_color_theme/dynamic_color_theme.dart';

You can also utilize the built-in color picker dialog by importing as shown below.

import 'package:dynamic_color_theme/color_picker_dialog.dart';

Usage

To use it, wrap your main widget like so:

const kFuchsia = const Color(0xFF880E4F);
const kWhite = Colors.white;
const kLightGrey = const Color(0xFFE8E8E8);
const kDarkGrey = const Color(0xFF303030);

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return DynamicColorTheme(
      data: (Color color, bool isDark) {
        return _buildTheme(color, isDark); // TODO define your own buildTheme method here
      },
      defaultColor: kFuchsia,
      defaultIsDark: false,
      themedWidgetBuilder: (BuildContext context, ThemeData theme) {
        return MaterialApp(
          home: MyHomePage(title: 'Dynamic Color Theme'),
          theme: theme,
          title: 'Flutter Demo',
        );
      },
    );
  }

  // Example buildTheme method
  ThemeData _buildTheme(Color accentColor, bool isDark) {
    final ThemeData base = isDark ? ThemeData.dark() : ThemeData.light();
    final Color primaryColor = isDark ? kDarkGrey : kWhite;

    return base.copyWith(
      accentColor: accentColor,
      accentTextTheme: _buildTextTheme(base.accentTextTheme, accentColor),
      cardColor: primaryColor,
      floatingActionButtonTheme: base.floatingActionButtonTheme.copyWith(
        backgroundColor: accentColor,
      ),
      iconTheme: base.iconTheme.copyWith(
        color: accentColor,
      ),
      primaryColor: primaryColor,
      primaryIconTheme: base.primaryIconTheme.copyWith(
        color: accentColor,
      ),
      primaryTextTheme: _buildTextTheme(base.primaryTextTheme, accentColor),
      scaffoldBackgroundColor: primaryColor,
      textSelectionTheme: _buildTextSelectionTheme(base.textSelectionTheme, accentColor, isDark),
      textTheme: _buildTextTheme(base.textTheme, accentColor),
    );
  }

  TextTheme _buildTextTheme(TextTheme base, Color color) {
    return base.copyWith(
      bodyText2: base.bodyText2.copyWith(
        fontSize: 16,
      ),
      bodyText1: base.bodyText1.copyWith(
        color: color,
        fontSize: 16,
        fontWeight: FontWeight.bold,
      ),
      button: base.button.copyWith(
        color: color,
      ),
      caption: base.caption.copyWith(
        color: color,
        fontSize: 14,
      ),
      headline5: base.headline5.copyWith(
        color: color,
        fontSize: 24,
      ),
      subtitle1: base.subtitle1.copyWith(
        color: color,
        fontSize: 18,
      ),
      headline6: base.headline6.copyWith(
        color: color,
        fontSize: 20,
        fontWeight: FontWeight.bold,
      ),
    );
  }

  TextSelectionThemeData _buildTextSelectionTheme(TextSelectionThemeData base, Color accentColor, bool isDark) {
    return base.copyWith(
      cursorColor: accentColor,
      selectionColor: isDark ? kDarkGrey : kLightGrey,
      selectionHandleColor: accentColor,
    );
  }
}

You can change the theme yourself by utilizing the provided functions as shown below.

void changeColor(Color newColor) {
  DynamicColorTheme.of(context).setColor(
    color: newColor,
    shouldSave: true, // saves it to shared preferences
  );
}

void changeIsDark(bool isDark) {
  DynamicColorTheme.of(context).setIsDark(
    isDark: isDark,
    shouldSave: true, // saves it to shared preferences
  );
}

void resetTheme() {
  DynamicColorTheme.of(context).resetToSharedPrefsValues();
}

Dialog Widget

Alternatively, a color picker dialog widget is included that will do this for you. Use it like so:


// With the barrier dismissible

showDialog(
  barrierDismissible: false,
  builder: (BuildContext context) {
    return ColorPickerDialog(
      defaultColor: kFuchsia,
      defaultIsDark: false,
    );
  },
  context: context,
);

// Without the barrier dismissible

showDialog(
  builder: (BuildContext context) {
    return WillPopScope(
      child: ColorPickerDialog(
        defaultColor: kFuchsia,
        defaultIsDark: false,
      ),
      onWillPop: () async {
        // Handles resetting if user taps off dialog
        DynamicColorTheme.of(context).resetToSharedPrefsValues();
        return true;
      },
    );
  },
  context: context,
);

所屬分類

依賴

依賴清單 2 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。