パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v0.2.0

reactives

StatefulWidget の共通ロジックを抽出・グループ化・再利用する新しい方法。React フックのようなものですが、Flutter 向けです。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
38
いいね数
44
Pub ポイント
140

パッケージをインストール

flutter pub add reactives

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

ReactiveS は、React にインスパイアされたフックのようなパターンを使用して、StatefulWidget コンポーネント内の状態付きロジックを抽出・グループ化・再利用できる、Flutter/Dart パッケージです。複雑な状態管理を単純化し、再利用可能で合成可能な状態付き振る舞いのユニットを可能にします。クリーンでモジュール式のコードを実現するように設計されており、関心の分離を促進し、ボイラープレートを削減します。すべての主要プラットフォームをサポートしており、外部依存性なしに構造的な状態処理が必要なクロスプラットフォームアプリに最適です。

用途

  • ウィジェット間で再利用可能な状態ロジック
  • 複雑な 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.

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。