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

套件詳情v0.2.0

reactives

一種提取/分組/重用 StatefulWidget 常見邏輯的新方式。可將其視為 Flutter 的 React Hooks。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
38
喜歡數
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.

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。