搜索分类、插件与代码仓

GitHub 代码仓matthewrice345/theme_manager

theme_manager

一个支持浅色、深色和系统主题的 Flutter 主题管理器。

theme_manager 项目封面
星标
7
复刻
1
最近推送(UTC)
2024年2月20日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

theme_manager

A theme manager for light, dark, and system themes.

A theme manager that supports light, dark, and system default themes. State is retained and applied upon application start. Heavily inspired by dynamic_theme by Norbert Kozsir.

Getting Started

Add theme_manager to your project.

  dependencies:
    theme_manager: ^2.0.4

run flutter packages get and import theme_manager

import 'package:theme_manager/theme_manager.dart';

A dialog is provided that can switch between themes.

import 'package:theme_manager/theme_picker_dialog.dart';

How to use

Make sure WidgetsFlutterBinding are initialized. This is required.

void main() {
  WidgetsFlutterBinding.ensureInitialized(); // Required
  runApp(MyApp());
}

Wrap your MaterialApp with ThemeManager:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ThemeManager(
      defaultBrightnessPreference: BrightnessPreference.system,
      data: (Brightness brightness) => ThemeData(
        primarySwatch: Colors.blue,
        accentColor: Colors.lightBlue,
        brightness: brightness,
      ),
      loadBrightnessOnStart: true,
      themedWidgetBuilder: (BuildContext context, ThemeData theme) {
        return MaterialApp(
          title: 'Theme Manager Demo',
          theme: theme,
          home: MyHomePage(),
        );
      },
    );
  }
}

When you want to change your theme:

void setAsSystemDefault() => 
  ThemeManager.of(context).setBrightnessPreference(BrightnessPreference.system);
void setAsLight() => 
  ThemeManager.of(context).setBrightnessPreference(BrightnessPreference.light);
void setAsDark() => 
  ThemeManager.of(context).setBrightnessPreference(BrightnessPreference.dark);

The system default will load either light or dark based on the device preferences. If your device changes themes at sunset or sunrise then light/dark mode will apply automatically.

The BrightnessPreference is saved in SharedPreferences automatically. There is also a clear function to remove the preferences.

void clear() => ThemeManager.of(context).clearBrightnessPreference();
A dialog widget to change the brightness!

Getting Started

For help getting started with Flutter, view our online documentation.

For help on editing package code, view the documentation.

Matthew Rice GitHub avatar
GitHub 作者

Matthew Rice ↗

语言DartRubyObjective-CJava

此仓库发布的插件

使用的依赖

依赖清单 6 项

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗