states_rebuilder 0.1.2

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new71

states_rebuilder #

A Flutter state management solution that allows you:

  • to separate your User Interface (UI) representation from your logic classes
  • to easily control how your widgets rebuild to reflect the actual state of your application.

This Library provides two classes and one method:

  • The StatesRebuilder class. Your logics classes will extend this class to create your own business logic BloC (equally can be called ViewModel or Model).
  • The rebuildStates method. You call it inside any of your logic classes that extends StatesRebuilder. It offers you two ways to rebuild any of your widgets. this is the signature of the rebuildState:
  rebuildStates({
      VoidCallback setState, // an optional VoidCallback to execute inside the Flutter setState() method 
      List<String> ids // First way to rebuild a particular widget indirectly by giving its id
      List<State> states, // Second way to rebuild a particular widget directly by giving its State
    })
  • The StateBuilder Widget. You wrap any part of your widgets with it to make it available inside your logic classes and hence can rebuild it using rebuildState method this is the constructor of the StateBuilder:
  StateBuilder( {
      Key key, 
      String stateID, // you define the ID of the state. This is the first way
      List<StatesRebuilder> blocs, // You give a list of the logic classes (BloC) you want this ID will be available.
      @required (State) → Widget builder,  // You define your top most Widget.
      (State) → void initState, // for code to be executed in the initState of a StatefulWidget
      (State) → void dispose, // for code to be executed in the dispose of a StatefulWidget
      (State) → void didChangeDependencies, // for code to be executed in the didChangeDependencies of a StatefulWidget
      (StateBuilder, State) → void didUpdateWidget // for code to be executed in the didUpdateWidget of a StatefulWidget
    });

For the first way you have to provide the stateID and blocs parameters. Whereas for the second way you have not. See prototype example bellow.

Prototype Example #

your_bloc.dart file:

  import 'package:flutter/material.dart';
  import 'package:states_rebuilder/states_rebuilder.dart'

  class YourBloc extends StatesRebuilder{

      var yourVar;

      /// You have two ways:

      /// ************** First way: (ID way) **************

      yourMethod1() {
        // some logic staff;
        yourVar = yourNewValue;
        rebuildStates(ids : [“yourStateID1”]);
      }

      // example of fetching data and rebuilding widgets after obtaining the data
      fetchData1() async {
        await yourRepository.fetchDate();
        rebuildStates(ids : [“yourStateID1”]);
      }

      /// ************** Second way (state way) **************

      yourMethod2(State state) {
        // some logic staff;
        yourVar = yourNewValue;
        rebuildStates(states : [state]);
      }

      // example of fetching data and rebuild widgets after obtaining the data
      fetchData2(State state) async {
        await yourRepository.fetchDate();
        rebuildStates(states : [state]);
      }

      /// ************** Combination of first and second ways **************

      yourMethod3(State state) {
        // some logic staff;
        yourVar = yourNewValue;
        rebuildStates(states : [state], ids : [“yourStateID1”]);
      }
  }

your main.dart file:

  // ************** First way: (ID way) ************** 
  class Firstway extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return Column(
            children: <Widget> [
              StateBuilder(
                stateID : "yourStateID1",
                blocs : [yourBloc],
                initState: (_)=> yourBloc.fetchData1(),
                builder: (_) => YourChildWidget(yourBloc.yourVar),
            ),
            RaisedButton(
              child: Text("first way"),
              onPressed : yourBloc.yourMethod1,
            )
          ],
      );
    }
  }

    // ************** Second way: (ID way) ************** 
    class Secondway extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return StateBuilder(
              initState: yourBloc.fetchData2,
              builder: (State state) => Column(
                    children: <Widget> [
                      YourChildWidget(yourBloc.yourVar),
                      RaisedButton(
                        child: Text("Second way"),
                        onPressed :yourBloc.yourMethod2(state),
                      ), 
                    ],
                  ),
              );
    }
  }

0.1.2 #

  • Remove print statements

0.1.1 #

  • Change readme.md of the example

0.1.0 #

  • Initial version

example/README.md

counter_app #


import 'package:flutter/material.dart';
import 'package:states_rebuilder/states_rebuilder.dart';

// Our logic class a counter variable and a method to increment it.
//
// It must extend from StatesRebuilder.
class CounterBloc extends StatesRebuilder {
  int _counter1 = 0;
  int _counter2 = 0;

  int get counter1 => _counter1;
  int get counter2 => _counter2;

  void increment1() {
    // First, increment the counter
    _counter1++;

    // First alternative.
    // Use the ids parameters to enter a list of ids.
    // Widgets with these ids will rebuild to reflect the new counter value.
    rebuildStates(ids: ["myCounter"]);
  }

  void increment2(State state) {
    // First, increment the counter
    _counter2++;

    // First alternative.
    // Use the ids parameters to enter a list of ids.
    // Widgets with these ids will rebuild to reflect the new counter value.
    rebuildStates(states: [state]);
  }
}

// For simplicity I use this method to provide the CounterBloc:
// Declare the counterBloc without instantiating it
//
// NOTE: You can use the InheritedWidget to provide the CounterBloc
CounterBloc counterBloc;



void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // At the top level of our widget tree where we want to provide the CounterBloc to all its children, we create a StateBuilder Widget.
    // We instantiate the counterBloc variable in the initState parameter, and kill it in the dispose parameter.
    return StateBuilder(
      initState: (_) => counterBloc = CounterBloc(),
      dispose: (_) => counterBloc = null,
      builder: (_) => MaterialApp(
            title: 'states_rebuilder Example',
            home: Counter('States_Rebuilder demostration'),
          ),
    );
  }
}

class Counter extends StatelessWidget {
  final String title;

  Counter(this.title);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text('You have pushed the button this many times:'),
            Divider(),
            Text('The first alternative'),

            // First Alternative:
            // -- Wrap the Text widget with StateBuilder widget and give it and id of your choice.
            // -- Declare the blocs where you want the state to be available.
            StateBuilder(
              stateID: 'myCounter',
              blocs: [counterBloc],
              builder: (State state) => Text(
                    counterBloc.counter1.toString(),
                    style: Theme.of(state.context).textTheme.display1,
                  ),
            ),

            // The first method is mainly useful if you want to increment the counter from other widget
            IncrementFromOtheWidget(),
            Divider(),
            Text('The second alternative'),

            // Second Alternative:
            // -- Wrap the Text widget with StateBuilder widget without giving it an id.
            // -- Declare the blocs where you want the state to be available.
            StateBuilder(
              builder: (State state) => Column(
                    children: <Widget>[
                      Text(
                        counterBloc.counter2.toString(),
                        style: Theme.of(state.context).textTheme.display1,
                      ),
                      RaisedButton(
                        key: Key("secondAlternative"),
                        child: Text("increment the same widget"),
                        onPressed: () => counterBloc.increment2(state),
                      )
                    ],
                  ),
            ),
          ],
        ),
      ),
    );
  }
}

class IncrementFromOtheWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      key: Key("firstAlternative"),
      child: Text("increment from other widget"),
      onPressed: counterBloc.increment1,
    );
  }
}


Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  states_rebuilder: ^0.1.2

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:states_rebuilder/states_rebuilder.dart';
  
Version Uploaded Documentation Archive
0.1.2 Mar 1, 2019 Go to the documentation of states_rebuilder 0.1.2 Download states_rebuilder 0.1.2 archive
0.1.1 Mar 1, 2019 Go to the documentation of states_rebuilder 0.1.1 Download states_rebuilder 0.1.1 archive
0.1.0 Mar 1, 2019 Go to the documentation of states_rebuilder 0.1.0 Download states_rebuilder 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
41
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
71
Learn more about scoring.

We analyzed this package on Mar 6, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.2.0
  • pana: 0.12.14
  • Flutter: 1.3.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test