搜索插件、代码仓与 APP 分类

easy_dynamic_theme 封面
插件详情v2.3.1

easy_dynamic_theme

自动获取操作系统定义的主题(动态),强制使用您偏好的主题(浅色 / 深色),并持久保存您的选择。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1480
喜欢数
125
Pub 分数
150

安装插件

flutter pub add easy_dynamic_theme

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

EasyDynamicTheme 是一个 Flutter 包,通过自动检测系统首选主题(浅色或深色模式),允许开发者强制指定特定主题,并在应用重启后持久化用户偏好,从而简化主题管理。它支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux。该包与 Flutter 的主题系统无缝集成,减少样板代码,并通过自适应设计提升用户体验。非常适合希望根据用户设置和设备行为提供一致且个性化 UI 的应用程序。

适合场景

  • 基于系统主题的自适应用户界面
  • 保持用户主题偏好
  • 强制使用浅色或深色模式
  • 跨平台主题一致性
  • 在大型应用中简化主题逻辑
  • 支持无障碍功能

主要特点

  • 自动操作系统主题检测
  • 持久化主题存储
  • 轻松切换主题
  • 支持所有 Flutter 平台
  • 所需设置最少
  • 内置主题提供程序

适合对象

  • 寻求简单主题管理的开发者
  • 需要深色/浅色模式支持的应用
  • 针对多个平台的项目
  • 希望减少样板代码的团队
  • 专注于用户体验的应用程序
  • 注重无障碍的应用程序

使用建议

  • 需要手动处理主题重建
  • 超出基础主题的自定义功能有限
  • 不支持运行时动态主题更新
  • 最适合具有静态主题选择的应用程序
  • 可能需要额外的逻辑来处理复杂的主题场景
  • 依赖 shared_preferences 实现持久化

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Easy Dynamic Theme

This is a new and easy approach on Flutter Themes.

Automatically get your OS defined Theme (Dynamic), force your prefered one (Light / Dark) and persist your choice in the device.

Pub Version flutter tests Codacy Badge codecov GitHub BuyMeACoffee

Easy peasy, don't you think? ;)

Demo

Getting Started

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Minimum Requirements

  • Dart SDK: >=2.12.0 <3.0.0
  • Flutter: >= 1.20.0

Installation and Usage

Once you're familiar with Flutter you may install this package adding easy_dynamic_theme to the dependencies list
of the pubspec.yaml file as follow:

dependencies:  
  flutter: 
    sdk: flutter  
 easy_dynamic_theme: ^2.2.0

Then run the command flutter packages get on the console.

Examples of use

All magic occurs in your main.dart file

import 'package:flutter/material.dart';
import 'package:easy_dynamic_theme/easy_dynamic_theme.dart';
import 'home.dart';

void main() async { 
  runApp( 
    EasyDynamicThemeWidget( 
      child: MyApp(), 
    ), 
  );
}  
  
class MyApp extends StatelessWidget {  
  final String title = 'EDT - Example'; 
   
  @override 
  Widget build(BuildContext context) { 
    return MaterialApp( 
      title: title, 
      theme: ThemeData.light(), 
      darkTheme: ThemeData.dark(), 
      themeMode: EasyDynamicTheme.of(context).themeMode, 
      home: new MyHomePage(title: title,) 
    ); 
  }
}  
How to use predefined Themes

You can use your own themes as follows:

In the file themes.dart define your Themes

  import 'package:flutter/material.dart';
  
  var lightThemeData = new ThemeData(
      primaryColor: Colors.blue,
      textTheme: new TextTheme(button: TextStyle(color: Colors.white70)),
      brightness: Brightness.light,
      accentColor: Colors.blue);
  
  var darkThemeData = ThemeData(
      primaryColor: Colors.blue,
      textTheme: new TextTheme(button: TextStyle(color: Colors.black54)),
      brightness: Brightness.dark,
      accentColor: Colors.blue);

And then in your main.dart file import your themes file and use them on MaterialApp attributes theme and darkTheme

import 'package:flutter/material.dart';
import 'package:easy_dynamic_theme/easy_dynamic_theme.dart';
import 'theme.dart';
import 'home.dart';

void main() async { 
  runApp( 
    EasyDynamicThemeWidget( 
      child: MyApp(), 
    ), 
  );
}  
  
class MyApp extends StatelessWidget {  
  final String title = 'EDT - Example'; 
   
  @override
  Widget build(BuildContext context) { 
    return MaterialApp( 
      title: title, 
      theme: lightThemeData, 
      darkTheme: darkThemeData, 
      themeMode: EasyDynamicTheme.of(context).themeMode, 
      home: new MyHomePage(title: title,) 
    ); 
  }
}  
How to change the ThemeMode in your app

You can use the function changeTheme from anywhere in your app.

This function have two optional parameters: dynamic and dark. If the value of dynamic is true, it takes precedence over dark.

  EasyDynamicTheme.of(context).changeTheme();
How to get your app current theme
Current app ThemeMode
ThemeMode themeMode = EasyDynamicTheme.of(context).themeMode;  

The above example will return a value of the enum used by MaterialApp's ThemeMode with one of the following values:

system - Use either the light or dark theme based on what the user has selected in the system settings.

light - Always use the light mode regardless of system preference.

dark - Always use the dark mode (if available) regardless of system preference.

Current Context brightness
Brightness brightness = Theme.of(context).brightness;  

Or if you want to know if your widget is dark mode based, you can achieve it with:

bool isDarkModeOn = Theme.of(context).brightness == Brightness.dark;  
What about some out-of-the-box widgets?
Right now we have
  • EasyDynamicThemeBtn, which is (kind of) a FlatButton that displays the icon according to the current theme of your app and allows you to switch between them.
  • EasyDynamicThemeSwitch, which is a Switch widget, based on the current theme's brightness of your context and allows you to switch between light/dark them.
  • EasyDynamicThemeAutoSwitch, which is a Switch widget, based on the current theme of your app and allows you to prioritize (or not) your OS defined brightness.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目