搜尋分類、套件與程式碼儲存庫

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 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。