Hooks but for and of Flutter

Related tags

Templates reactives
Overview

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.

You might also like...

A Flutter based to do list app (yes, another to do list... but... this time based on a beautiful design)

A Flutter based to do list app (yes, another to do list... but... this time based on a beautiful design)

✔️ Flutter to do App "To Do List" A Flutter app based on the design of the To Do App, created by Rudi Hartono, see more on: Uplabs. Getting Started 🚀

Dec 31, 2022

BubbleShowcase is a small but power flutter package that allows you to highlight specific parts of your app to explain them to the user or to showcase your app new features.

BubbleShowcase BubbleShowcase is a small but powerful flutter package that allows you to highlight specific parts of your app (to explain them to the

Oct 26, 2022

The same old Weather App, But 'In Flutter , By Me'

The same old Weather App, But 'In Flutter , By Me'

Weather App v1.0.1 About The same old Weather App, But 'In Flutter , By Me' . NB Still in developement Stats ### v1.0.1 - Started a Basic outline of

Sep 7, 2022

An easy-to-use flutter http network requests handler with more functionality than http but more simpler than dio.

network_requests An easy-to-use flutter http network requests handler with more functionality than http but more simpler than dio. Platform Supported

Dec 15, 2022

Overlay/OverlayEntry, but better

Overlay/OverlayEntry, but better

Call for maintainer I (@rrousselGit) am sadly unable to maintain flutter_portal at the moment due to a lack of time, and would like to find a new main

Jan 2, 2023

Totally *legal* Instagram automation, crosses your user follower/following data to find who you follow, but doesnt follow you

untruth-instagram-followers Totally *legal* Instagram automation. Crosses a user follower/following data to find who he follows, but that doesn't foll

Oct 12, 2022

A beautiful, secure and simple authenticator app that supports multiple protocols and services. Free and open source. Written in Flutter and Dart.

A beautiful, secure and simple authenticator app that supports multiple protocols and services. Free and open source. Written in Flutter and Dart.

OpenAuth A beautiful, secure and simple authenticator app that supports multiple protocols and services. Free and open source. Written in Flutter and

Oct 5, 2022

This project has the vision to assist the officials for Forest trees census and tagging each tree with proper location (latitude and longitude), tree type, and other arguments. and further had the plan to apply data analysis over-collected data.

🌳 Trees 🌳 🔖 Tagger 🔖 App & Analysis Software The vision of this project is to assist forest officials for tree census by tagging each tree with pr

Sep 29, 2022
Comments
  • Curious if it's possible to use this library together with my get_it_mixin

    Curious if it's possible to use this library together with my get_it_mixin

    Hi, just stumbled upon this new package.

    My get_it_mixin is a simpler hooks like package to observe objects inside get_it.

    If I understand it correctly you still need stateful widgets to make it work. I think it could indeed work. @esDotDev what do you think?

    opened by escamoteur 3
  • Add primitives

    Add primitives

    Added some value types for easily rebuilding the view when data changes.

    • ReactiveInt
    • ReactiveBool
    • ReactiveString
    • ReactiveDouble
    • ReactiveValue<T>

    Address this issue: https://github.com/ripemango/reactives/issues/2

    Tweaked the example to make use of the bool reactive for showPassword:

    class ReactiveLoginLogic extends Reactive {
      ReactiveLoginLogic(ReactiveHost host) : super(host);
      late final ReactiveTextEditingController _email = ReactiveTextEditingController(host);
      late final ReactiveTextEditingController _password = ReactiveTextEditingController(host);
      late final ReactiveBool _showPasswordReactive = ReactiveBool(host); // rebuilds when changed
    
      TextEditingController get emailCtrl => _email.ctrl;
      TextEditingController get passwordCtrl => _password.ctrl;
      bool get showPassword => _showPasswordReactive.value;
    
      // Login logic
      void submit() {}
    
      void toggleShowPassword() => _showPasswordReactive.value = !showPassword;
    }
    
    opened by esDotDev 0
  • Proposal: Add reactive primitives like ReactiveInt, ReactiveBool etc

    Proposal: Add reactive primitives like ReactiveInt, ReactiveBool etc

    Adding primitives would be nice as they could automatically call setState when their value has changed, as well as potentially implement restoration so the developer does not have to.

    opened by esDotDev 0
Releases(v0.2.0)
Owner
Ripe Mango
Ripe Mango ❤️ Open Source
Ripe Mango
Simple and complete Flutter hooks testing utilities that encourage good testing practices.

Flutter Hooks Testing Library Simple and complete Flutter hooks testing utilities that encourage good testing practices. Inspired by react-hooks-testi

Daichi Furiya 24 Dec 2, 2022
Coffee flutter - A coffee app Used Firestore, GetX, flutter hooks, Provider

coffee_flutter A Coffee Flutter project. Used Firestore, GetX, flutter_hooks, Pr

deargo 4 Oct 26, 2022
An opinionated, community-driven set of lint rules for Dart and Flutter projects. Like pedantic but stricter

Lint for Dart/Flutter lint is a hand-picked, open-source, community-driven collection of lint rules for Dart and Flutter projects. The set of rules fo

Pascal Welsch 257 Jan 3, 2023
ValueNotifier, but outside Flutter and with some extra perks

Welcome to state_notifier~ This package is a recommended solution for managing state when using Provider or Riverpod. Long story short, instead of ext

Remi Rousselet 290 Dec 24, 2022
A Simple but elegant Calculator app made with Flutter using Google's Material Design with Currency (Exchange Rate) and Unit Converter.

Calculator A Simple but elegant Calculator app made with Flutter using Google's Material Design with Currency (Exchange Rate) and Unit Converter. Down

Hemant Rajput 15 Dec 7, 2022
Automatically generate profile picture with random first name and background color. But you can still provide pictures if you have them. As the default color, based on the name of the first letter. :fire: :fire: :fire:

FLUTTER PROFILE PICTURE Automatically generate profile picture with random first name and background color. But you can still provide pictures if you

Aditya Dharmawan Saputra 10 Dec 20, 2022
Advanced & beautiful animations inspired by animate_do and Animate.css, every animation is a widget that contains default but customizable values 💙

animate_it An animation package inspired by animate_do and Animate.css. Since the original animate_do is not being maintained, I have decided to creat

Luke Moody 3 Oct 1, 2022
Album Image is based in photo_manager package and has the same concept as image_picker but with a more attractive interface to choose an image or video from the device gallery, whether it is Android or iOS

Album Image is based in photo_manager package and has the same concept as image_picker but with a more attractive interface to choose an image or vide

Phuong Vu 2 Oct 13, 2022
This is an open source Tips & Tricks for Flutter, that helps flutter Developers write simple but powerful dart codes.

Showcasing-flutter - This is an open source Tips & Tricks for Flutter, that helps flutter Developers write simple but powerful dart codes.

Paul Edeme'kong - Flutter Fairy 1 Jan 4, 2022
Simple but pretty cool birthday countdown app built using flutter

Simple but pretty cool birthday countdown app "For You" ?? Don't forget to star ⭐ the repo if you like what you I have created ?? . ?? GIF of a sample

Ruslan Hasanov 6 Aug 13, 2022