パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v3.0.0

theme_mode_handler

実行時に `themeMode` を変更し、再起動間で永続化するための Flutter ウィジェット

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

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト