搜索插件、代码仓与 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 项

源码仓库

使用此插件的项目