搜尋套件、儲存庫與 APP 分類

flutter_triple 封面
套件詳情v3.0.0

flutter_triple

分段狀態模式(SSP)的實現,綽號為 Triple。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
4650
喜歡數
125
Pub 分數
140

安裝套件

flutter pub add flutter_triple

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Flutter Triple 是一種實現分段狀態模式(SSP)的狀態管理解決方案,常被稱為「Triple」。它透過將資料、載入與錯誤狀態分離,提供一種清晰且可預測的方式來管理應用程式狀態。此套件設計簡潔且具可擴展性,能無縫整合至 Flutter 的反應式生態系統中。該套件促進程式碼組織並減少重複程式碼,非常適合尋求輕量級但強大替代傳統狀態管理方式的開發者。它支援所有主要平台,包含 Android、iOS、Web、macOS、Windows 與 Linux。

適用情境

  • 在Flutter應用中管理複雜的UI狀態
  • 使用明確的載入與錯誤狀態處理非同步作業
  • 建構可擴展且可維護的狀態邏輯
  • 簡化反應式 UI 中的狀態轉換
  • 與依賴注入框架整合
  • 減少狀態管理中的樣板程式碼

主要特色

  • 狀態類型(初始、成功、載入、失敗)的清晰分離
  • 對異步操作的內建支援
  • 透過 StreamBuilder 與 ValueListenableBuilder 實現反應式更新
  • 最小化樣板程式碼,提供直覺的 API
  • 可組合且可擴展的狀態模式
  • 跨平台相容性

適合對象

  • 尋求輕量級狀態管理解決方案的開發者
  • 希望在各個專案中實現一致的狀態管理的團隊
  • 頻繁執行非同步操作的應用程式
  • 使用依賴注入的專案
  • 初學者學習狀態管理模式
  • 需要可維護狀態邏輯的大規模應用

使用建議

  • 需要理解三元組模式的概念
  • 進階用法可能需要額外的設定
  • 與完整框架相比,內建的除錯工具較少
  • 最適合狀態複雜性足以證明其結構合理的應用
  • 對於不需要異步邏輯的非常簡單的狀態需求來說,不是理想選擇
  • 依賴外部函式庫以實現完整功能

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Triple - Segmented State Pattern


Logo Logo

Welcome to Triple! Design Pattern for State Management.

· Report Bug · Request Feature



Table of Contents
  1. About The Project
  2. Usage
    1. Installation
    2. How to use?
  3. Contributing
  4. Contact
  5. Contributors


:memo: About The Project

What is Triple?

Triple is a nickname to SSP (Segmented State Standard). Some packages were created to make it easier for developers to start using the standard. We'll call it an extension.

Segmented State Pattern (SSP)

When we talk about a single flow state, we end up solving architecture problems early, as we will have only one data flow for each state.

In addition to the maintainability and to the ease of use architecture, we also have the possibility of increasing this flow with other standards such as the Observer, which gives reactivity to the component when it is modified, and Memento, which makes it possible to revert or redo this state.

A beautiful example of a pattern with a single flow is BLoC, giving reactivity to a state allowing all transformations in that flow. This (although complicated for some), consolidates very well in the architecture of a project, even the limits of this practice are beneficial for not allowing the developer to resort to other solutions for the architecture and the standard for its resource.

There are other ways to promote reactivity in a property instead of the entire object, such as MobX's Observable and Flutter's ValueNotifier, and that gives us a lot of choices. However, we lose some important architecture limits, which can put in check the project maintenance in the future. Therefore, it needs a standard to force limits on the individual reactivity of each property and thereby improve the maintainability of the components responsible for managing the states of the application.

Triple is a nickname to SSP (Segmented State Standard). Some packages were created to make it easier for developers to start using the standard. We'll call it an extension.


This project is distributed under the MIT License. See LICENSE.txt for more information.


✨ Usage

The SSP segments the state into 3 reactive parts, the state value (state), the error object (error), and the state loading action (loading).

These segments are observed in a listener or separate listeners. They can also be combined to obtain a new segment, always starting from the 3 main segments.

Installation

Dependencies

add the following dependencies to your pubspec.yaml file:

dependencies:
  flutter_triple: ^1.3.0
Import

Import the package in your code with:

import 'package:flutter_triple/flutter_triple.dart';

How to use?

Create a Store

Create a class that extends NotifierStore<Error, State>. The first type is the type of the error, the second is the type of the state.

class CounterStore extends NotifierStore<Exception, int> {
  CounterStore() : super(0);

  void increment() => update(state + 1);
  void decrement() => update(state - 1);
}
Consume with Listeners, Builders and Consumers

Consume the store with scopes ScopedBuilder, ScopedListener and ScopedConsumer.


class CounterPage extends StatefulWidget {
  @override
  _CounterPageState createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  final store = CounterStore();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Counter'),
      ),
      body: ScopedBuilder<CounterStore, Exception, int>(
        store: store,
        onLoading: (context) => Center(child: CircularProgressIndicator()),
        onError: (context, error) => Center(child: Text(error.toString())),
        onState: (context, state) => Center(
          child: Text(
            '$state',
            style: Theme.of(context).textTheme.headline4,
          ),
        ),
      ),
      floatingActionButton: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            FloatingActionButton(
              onPressed: store.decrement,
              tooltip: 'Decrement',
              child: Icon(Icons.remove),
            ),
            SizedBox(width: 5),
            FloatingActionButton(
              onPressed: store.increment,
              tooltip: 'Increment',
              child: Icon(Icons.add),
            ),
          ],
        ),
    );
  }
}

🧑‍💻 Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the appropriate tag. Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Remember to include a tag, and to follow Conventional Commits and Semantic Versioning when uploading your commit and/or creating the issue.

(back to top)

💬 Contact

Flutterando Community

(back to top)


👥 Contributors

(back to top)

🛠️ Maintaned by


This fork version is maintained by Flutterando.

所屬分類

依賴

依賴清單 3 項

原始碼儲存庫

使用此套件的專案