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

appearance 封面
插件详情v0.0.1

appearance

Flutter 包,用于实现亮色、暗色和系统主题模式,并在应用重启时保持设置。

AndroidiOSWeb
近 30 天下载
59
喜欢数
5
Pub 分数
150

安装插件

flutter pub add appearance

插件介绍

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

外观包简化了在Flutter应用中实现浅色、深色和系统主题模式的方法。它提供了一种无缝切换主题的方式,并可在应用重启后保留所选模式。该包设计时注重集成的便捷性,负责主题状态管理,并确保在Android、iOS和Web平台上提供一致的用户体验。该包支持动态主题切换,并能自动适应系统级别的主题偏好。

适合场景

  • 在移动和网页应用中实现主题切换
  • 在应用重启后保留用户的主题偏好
  • 支持系统主题感知
  • 创建具有亮色和暗色模式的可访问性用户界面
  • 启用平滑的主题过渡

主要特点

  • 浅色、深色和系统主题支持
  • 持久的主题状态存储
  • 自动系统主题检测
  • 跨平台兼容性(Android、iOS、Web)
  • 主题切换的简单 API

适合对象

  • Flutter 开发人员构建跨平台应用
  • 需要用户自定义主题的应用程序
  • 专注于无障碍和用户体验的项目
  • 希望主题化代码量最小的开发者
  • 同时针对移动和网页的应用程序

使用建议

  • 需要手动处理特定主题的控件样式
  • 不包含内置主题设计组件
  • 仅限主题模式持久化,不支持完整主题自定义
  • 可能需要额外设置以处理复杂的主题逻辑
  • 如果超出基本功能使用,依赖外部状态管理

原始 README

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

appearance

Appearance

Flutter package that easily implement Light, Dark and System theme mode in your application and persists the theme mode on restart of the application.

🚀 Demo: Appearance

📱 Screenshots

Appearance Demo 1 Appearance Demo 2

🛠 Installation

  1. Add dependency to pubspec.yaml file:
    Get the latest version from the 'Installing' tab on pub.dev
dependencies:
  appearance: ^latest_version
  1. Import the package
import 'package:appearance/appearance.dart';

📘 How to use

  1. Initialize SharedPreferencesManager singleton for SharedPreference and add it before runApp() method.
void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SharedPreferencesManager.instance.init();
  runApp(const MyApp());
}
  1. extend your root class with AppearanceState using keyword with.
class _MyAppState extends State<MyApp> with AppearanceState { }
  1. Wrap your MaterialApp or CupertinoApp with BuildWithAppearance
BuildWithAppearance(
    initial: ThemeMode.light, // optional : default value is [ThemeMode.system]
    builder: (context) => MaterialApp()
);
Using MaterialApp
BuildWithAppearance(
    initial: ThemeMode.light, // optional : default value is [ThemeMode.system]
    builder: (context) => MaterialApp(
        title: 'Appearance (Material Example)',
        themeMode: Appearance.of(context)?.mode,
        theme: ThemeData(
            brightness: Brightness.light,
        ),
        darkTheme: ThemeData(
            brightness: Brightness.dark,
        ),
        home: const HomeMaterialPage(),
    ),
);
Using CupertinoApp
BuildWithAppearance(
    initial: ThemeMode.light, // optional : default value is [ThemeMode.system]
    builder: (context) {
        return CupertinoApp(
            title: 'Appearance (Cupertino Example)',
            theme: CupertinoThemeData(
                brightness: Appearance.of(context)?.cupertinoBrightness!,
            ),
            localizationsDelegates: const [
                DefaultMaterialLocalizations.delegate,
                DefaultCupertinoLocalizations.delegate,
                DefaultWidgetsLocalizations.delegate,
            ],
            home: HomeCupertinoPage(),
        );
    },
);

🌟 Features

  • Light, Dark and System mode options to change theme of app.

  • Theme persistance: Saved theme is persisted using SharedPreferences on restart.

  • Auto Listen Theme Mode changes, without adding extra listener.

  • Material and Cupertino, both apps are supported.

🧰 Parameters

  • initial: [optional]
    Set the initial ThemeMode of the app, by default its value is ThemeMode.system

Change Theme Mode

You can use the setMode method to change the theme mode of the app.

 // sets theme mode to System
 Appearance.of(context)?.setMode(ThemeMode.system),

 // sets theme mode to Light
 Appearance.of(context)?.setMode(ThemeMode.light),

 // sets theme mode to Dark
 Appearance.of(context)?.setMode(ThemeMode.dark),

Get Theme Mode

 // get active theme mode
 Appearance.of(context)?.mode

Get Cupertino Theme brightness

 // get active cupertino theme brightness
 Appearance.of(context)?.cupertinoBrightness!,

💻 Example

Check out the example app in the example directory for both Material and Cupertino.

📝Contribution

Feel free to contribute to this project.

  • If you found a bug or have a feature request, open an issue.

  • If you want to contribute, submit a pull request.

💳 License

This project is LICENSED under the MIT License. Use it freely, but let's play nice and give credit where it's due!

🎉 Conclusion

I will be happy to answer any questions that you may have on this approach,
If you liked this package, don't forget to show some ❤️ by smashing the ⭐.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目

暂无相关数据。