搜索插件、代码仓与 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

所属分类

源码仓库

使用此插件的项目