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

套件詳情v3.0.0

theme_mode_handler

Flutter 小部件,用於在執行時變更 `themeMode` 並在重新啟動之間持續保留它

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
215
喜歡數
19
Pub 分數
160

安裝套件

flutter pub add theme_mode_handler

套件介紹

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

一個 Flutter 套件,可在執行時動態切換主題模式,並在應用程式重啟後保留所選主題。它透過自動將使用者選擇儲存至共用偏好設定,簡化了明亮與黑暗模式的處理,確保應用程式重新開啟時的一致性。此組件可輕鬆整合至現有的 Flutter 應用程式中,設定簡單,無需手動儲存邏輯即可可靠地管理主題狀態。

適用情境

  • 移動應用中的動態主題切換
  • 在會話之間持續用戶主題偏好
  • 在Web應用程式中實現暗黑模式切換
  • 支援系統主題感知
  • 在桌面應用程式中啟用主題自訂

主要特色

  • 執行時期主題模式變更
  • 使用共享首選項的自動持久化
  • 與 MaterialApp 的簡單整合
  • 支援所有主要平台
  • 可設定的預設主題模式

適合對象

  • 開發帶有主題切換功能的應用程式之開發者
  • 需要持久化使用者主題設定的應用程式
  • 跨平台應用程式,針對多種設備

使用建議

  • 需要 shared_preferences 依賴
  • 僅限 themeMode(不支援完整主題自訂)
  • 可能需要為自定義主題添加額外邏輯
  • 不適用於超出模式切換之外的複雜主題管理

原始 README

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

theme_mode_handler

This library is considered done - no new features or changes are foreseen. You may not see recent updates to this repository, but the library is totally functional. Please open an issue if you find an issue or think something is missing so we can discuss it.

version All Contributors

Flutter widget to change themeMode during runtime and persist it across restarts.

Motivation

Flutter 1.9 introduced a new way to control which theme is used: MaterialApp.themeMode. If you have specified the darkTheme and theme properties, you can use themeMode to control it. The property defaults to ThemeMode.system.

This package wraps this functionality and allows you to persist and retrieve the user's preference wherever you want by implementing an interface.

Installation

Add this to your pubspec.yaml:

dependencies:
  theme_mode_handler: ^3.0.0

Usage

  • Create a class that implements the IThemeModeManager interface:
class MyManager implements IThemeModeManager {
  @override
  Future<String> loadThemeMode() async {}

  @override
  Future<bool> saveThemeMode(String value) async {}
}
  • Import the ThemeModeHandler widget, wrap MaterialApp with it and pass it an instance of your manager:
import 'package:theme_mode_handler/theme_mode_handler.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ThemeModeHandler(
      manager: MyManager(),
      builder: (ThemeMode themeMode) {
        return MaterialApp(
          themeMode: themeMode,
          darkTheme: ThemeData(
            brightness: Brightness.dark,
          ),
          theme: ThemeData(
            brightness: Brightness.light,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      },
    );
  }
}
  • Change the themeMode with:
ThemeModeHandler.of(context).saveThemeMode(value);
  • Get the current themeMode with:
ThemeModeHandler.of(context).themeMode;

Check the example folder for a complete example.

API

builder

Type: Widget Function(ThemeMode themeMode).

Function that runs when themeMode changes.

manager

Type: IThemeModeManager.

Implementation of IThemeModeManager to load and save the selected value.

defaultTheme

Type: ThemeMode.

Default value to be used when manager.loadThemeMode returns null or an invalid value.

placeholderWidget

Type: Widget?.

While the themeMode is loaded, you can choose to render a different widget. By default, it'll render an empty container.

Extra

This package exports a dialog and a method to simplify its usage.

  • Import the dialog with:
import 'package:theme_mode_handler/theme_picker_dialog.dart';
  • Use it like this:
void _selectThemeMode(BuildContext context) async {
  final newThemeMode = await showThemePickerDialog(context: context);
  print(newThemeMode);
}

Inspiration

This package is inspired and based on the great package dynamic_theme.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Arthur Denner

💻 🎨 💡 🚧

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT © Arthur Denner

所屬分類

原始碼儲存庫

使用此套件的專案