Search packages, repositories and app categories

Package detailsv0.2.0

reactives

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

AndroidiOSWebmacOSWindowsLinux
30-day downloads
38
Likes
44
Pub points
140

Install package

flutter pub add reactives

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

ReactiveS is a Flutter/Dart package that enables developers to extract, group, and reuse stateful logic in StatefulWidget components using a hook-like pattern inspired by React. It simplifies complex state management by allowing reusable, composable units of stateful behavior. Designed for clean, modular code, it promotes separation of concerns and reduces boilerplate. The package supports all major platforms, making it ideal for cross-platform apps needing structured state handling without external dependencies.

Use cases

  • Reusable state logic across widgets
  • Simplifying complex StatefulWidget classes
  • Cross-platform state management
  • Modular UI development
  • Reducing boilerplate in stateful components
  • Replacing repetitive state patterns

Key features

  • Hook-like syntax for Flutter
  • State logic extraction and reuse
  • Composable state units
  • Cross-platform support
  • Minimal boilerplate
  • Clean separation of state and UI

Suitable for

  • Developers seeking React-style hooks in Flutter
  • Teams wanting to reduce StatefulWidget complexity
  • Projects requiring modular state management
  • Cross-platform Flutter applications
  • Builders of reusable UI components

Considerations

  • Requires understanding of functional programming concepts
  • May introduce learning curve for beginners
  • Not a full state management solution
  • Best suited for smaller to medium-scale state logic
  • Limited documentation compared to mature state libraries

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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.

Categories

Source repository

Projects using this package

No data available yet.