T-Rex

Hooks, Statable widget, Tx (Trackable) variables and Datatype extensions to make development easier and efficient. T-Rex is a compact, simple State Management solution for your Flutter Applications, implemented without using Streams or any complicated abstractions.

Most State Management solutions use Streams to notify changes to data. This becomes highly unintuitive and inefficient when you have a lot of state variables and data changes to watch out for. T-Rex doesn't use Streams, and use basic features of Dart to provide a wide-variety of State Management solutions. T-Rex makes use of the underlying framework's methodologies to deliver a better result.

Hook<T> & Hookable widget

The code below demonstrates the usage of hooks using a Hookable widget. Your widget will automatically update UI when the value stored in the Hook changes. You can declare, initialize and use as many hooks as you want in your Hookable widget. This way of state management ensures that the state is attached to the widget, even when it is fragmented. Hookable is very similar to StatelessWidget and is lightweight. It doesn't have the overburden of a StatefulWidget and is therefore more performant.

class MyCounter extends Hookable {
  
  late final Hook<int> count;
  
  MyCounter() {
    count = Hook(0, this);
  }
  
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: crossAxisAlignment.center,
          children: [
            Text(count.value.toString()),
            IconButton(
              onPressed: () {
                /// The widget automatically rebuilds when value of the Hook changes.
                count.value++;
              },
              icon: Icon(Icons.add),
            ),
          ]
        ),
      ),
    );
  }
}

Statable widget

The statable widget is a direct replacement for StatefulWidget, but with one state variable, that can be of any datatype the developer wants to use. When the value of the state changes, the UI of the widget will automatically be refreshed by the Framework. This widget is similar to StatelessWidget in the implementation, but has the functionality of a StatefulWidget, without its heavy implementation.

class MyCounter extends Statable<int> {
  /// You must implement this function to use `Statable` widget
  void init() {
    this.state = 0;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(this.state.toString()),
            IconButton(
              onPressed: () {
                /// The widget automatically rebuilds when value of the `state` changes.
                this.state++;
              },
              icon: Icon(Icons.add),
            ),
          ],
        ),
      ),
    );
  }
}

Tx<T> Trackable Variables

The following method shows the usage of Tx variables in a normal StatefulWidget.

Tx<int> count = Tx(
    0,
    afterChange: (_) {
        this.setState(() {});
    }
);

The simplest way to manage state, for very simple widgets and screens.

Hookful<T>

This is a special type of implementation that enables the use of Hooks inside a StatefulWidget. The functionality of the Hookful is similar to Hook, but it is highly recommended that you use Hook instead of Hookful, since Hook has a widget type specifically implemented to suit its functionality and also ensures performance.

class MyCounter extends StatefulWidget {
  
  late Hookful<int> count;
  
  @override
  void initState() {
    count = Hookful(0, this);
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: crossAxisAlignment.center,
          children: [
            Text(count.value.toString()),
            IconButton(
              onPressed: () {
                /// The widget automatically rebuilds when value of the Hookful changes.
                count.value++;
              },
              icon: Icon(Icons.add),
            ),
          ]
        ),
      ),
    );
  }
}

Libraries

trex
T-Rex : Copyright 2021 : Aldrin Mathew