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

插件详情v0.2.0

reactives

一种提取/分组/重用 StatefulWidget 常见逻辑的新方法。可以将其视为 Flutter 的 React Hooks。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
37
喜欢数
44
Pub 分数
140

安装插件

flutter pub add reactives

插件介绍

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

ReactiveS 是一个 Flutter/Dart 包,使开发者能够使用受 React 启发的类似钩子的模式,从 StatefulWidget 组件中提取、分组和重用有状态逻辑。它通过允许可重用、可组合的状态行为单元,简化了复杂的状态管理。该包旨在实现干净、模块化的代码,促进关注点分离并减少样板代码。支持所有主要平台,非常适合需要结构化状态处理且无需外部依赖的跨平台应用。

适合场景

  • 跨组件的可重用状态逻辑
  • 简化复杂的 StatefulWidget 类
  • 跨平台状态管理
  • 模块化 UI 开发
  • 减少有状态组件中的样板代码
  • 替换重复的状态模式

主要特点

  • Flutter 的类似 Hook 的语法
  • 状态逻辑提取与复用
  • 可组合的状态单元
  • 跨平台支持
  • 最小化样板代码
  • 状态与 UI 的清晰分离

适合对象

  • 寻求在 Flutter 中使用类似 React 的钩子的开发者
  • 希望降低 StatefulWidget 复杂性的团队
  • 需要模块化状态管理的项目
  • 跨平台 Flutter 应用程序
  • 可重用 UI 组件的构建者

使用建议

  • 需要理解函数式编程概念
  • 可能为初学者引入学习曲线
  • 不是完整状态管理解决方案
  • 最适合较小到中等规模的状态逻辑
  • 与成熟状态的库相比,文档有限

原始 README

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

reactives

A new way for Flutter to reuse/group common logic. Think of them like React hooks but for and of flutter.

Idea copied from the lit world

Motive

Have you ever written code like this?

class AwesomeWidget extends StatefulWidget {
  const AwesomeWidget({Key? key}) : super(key: key);

  @override
  _AwesomeWidgetState createState() => _AwesomeWidgetState();
}

class _AwesomeWidgetState extends State<AwesomeWidget>
    with TickerProviderStateMixin {
  late final TextEditingController emailCtrl;
  late final TextEditingController passwordCtrl;
  late final AnimationController entryAnimation;
  late final AnimationController highLightAnimation;

  @override
  void initState() {
    super.initState();
    emailCtrl = TextEditingController();
    passwordCtrl = TextEditingController();
    entryAnimation = AnimationController(vsync: this);
    highLightAnimation = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    emailCtrl.dispose();
    entryAnimation.dispose();
    highLightAnimation.dispose();
    passwordCtrl.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

There are a couple of problems here.

  • Most of the object management logic is generic and can be reused.
  • It is extremely easy to forget that one dispose call (confession).
  • Coupling the logic to the widget makes it harder to test.

Using reactives this gets transformed to,

class AwesomeReactiveWidget extends StatefulWidget {
  const AwesomeReactiveWidget({Key? key}) : super(key: key);

  @override
  _AwesomeReactiveWidgetState createState() => _AwesomeReactiveWidgetState();
}

class _AwesomeReactiveWidgetState extends State<AwesomeReactiveWidget> with ReactiveHostMixin {
  late final emailCtrl = ReactiveTextEditingController(this);
  late final passwordCtrl = ReactiveTextEditingController(this);
  late final entryAnimation = ReactiveAnimationController(this);
  late final exitAnimation = ReactiveAnimationController(this);

  @override
  Widget build(BuildContext context) {
    return Container(
        // ....
    );
  }
}

See Examples for examples of writing a reactive or just browse the source, it's really small.

Comparision to flutter_hooks:

From a user perspective flutter_hooks is a replacement for StatefulWidget. reactives is not. If you look at the code for ReactiveHostMixin it is about 60 lines (blank lines included). Reactives do not try to replace StatefulWidget, it just solves the reusability problem inherent due to mixin's "is-a" relationship. Reactives have a "has-a" relationship.

There are no new rules to writing reactives. See examples of existing reactives. It is basically the same logic isolated in a different class. For the same reason it doesn't need a lot of the hooks like useState which would be needed if tried to replace StatefulWidget.

The learning curve of reactives is next to negligible. Hooks need you to learn a few concepts and how to/not to do things. It also requires you to transform entire widgets. Reactives can be adapted incrementally even for a single widget.

所属分类

源码仓库

使用此插件的项目

暂无相关数据。