state_bloc 1.0.4

  • Readme
  • Changelog
  • Example
  • Installing
  • 68

state_bloc #

pub package

StateBloc help the Widget tree to rebuild again with the help of streams. You do no need to create any bloc class for managing state, just need to instantiating the StateBloc class with an initial value.

Getting Started #

In your flutter project add the dependency:

state_bloc :  1.0.4

For help getting started with Flutter, view the online documentation.

Usage Example #

import 'package:state_bloc/state_bloc.dart';

Define a StateBloc #

//Requires a initial value in it's constructor 

StateBloc<int> stateBloc = StateBloc<int>(0);

Listen to Data using StateBlocBuilder #

You will need to pass the stateBloc and a widget to StateBlocBuilder

//Define the type of data you are using in StateBloc eg : int, String, Foo.

StateBlocBuilder<int>(
  stateBloc: stateBloc,
  widget: (int value) {
    return Text("$value");
  },
)

Change the state of the bloc #

1. Change the state when you have no access of the value #

This will provide you the previous state data present in the bloc other vise the state you have provided while instantiating the StateBloc

stateBloc.changeState(state: (value) {

  //do changes with value and return it
      return value;

});

2. Change the state without value provided #

stateBloc.value = value;

Or

stateBloc.changeStateWithoutValueProvided(value);

Listen to the value changed #

stateBloc.listenToValue(listenToValue: (int value) {

  //listen to the value when changed

});

Don't forget to dispose the bloc #

stateBloc.dispose();

Managing App State with get_it #

get_it Package pub package by escamoteur

In your flutter project add the dependency:

get_it: <latest-version>

Register the StateBloc with GetIt. #

GetIt appConfig = GetIt();

void setupAppConfig() {
//This will create a Singleton for the whole app
  appConfig.registerLazySingleton(
      () => StateBloc<DarkThemeModel>(DarkThemeModel()));
}

if you want to register the another StateBloc with GetIt , you will have to provide the instance name.

GetIt appConfig = GetIt();

void setupAppConfig() {
  appConfig.registerLazySingleton(
      () => StateBloc<DarkThemeModel>(DarkThemeModel()), "darkMode");
}

Register when the app runs. #

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

Using StateBloc instance from the GetIt #

StateBloc<DarkThemeModel> stateBloc= appConfig<StateBloc<DarkThemeModel>>("darkMode");

Kaboom 💣💣

Now you can change the app state with StateBloc with GetIt.

[0.0.1] - 18 Feb 2020. #

[0.0.2] - 18 Feb 2020. #

[0.0.3] - 18 Feb 2020. #

[0.0.4] - 18 Feb 2020. #

[0.0.5] - 18 Feb 2020. #

[0.0.6] - 18 Feb 2020. #

[0.0.7] - 18 Feb 2020. #

[0.0.8] - 18 Feb 2020. #

[1.0.0] - 18 Feb 2020. #

[1.0.1] - 19 Feb 2020. #

[1.0.2] - 20 Feb 2020. #

[1.0.3] - 2 April 2020. #

[1.0.4] - 12 April 2020. #

example/lib/main.dart

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Center(
        child: Container(
          child: Counter(),
        ),
      ),
    );
  }
}

class Counter extends StatefulWidget {
  _CounterState createState() => _CounterState();
}

class _CounterState extends State<Counter> {
  StateBloc<int> stateBloc = StateBloc<int>(0);

  void initState() {
    stateBloc.listenToValue(listenToValue: (int value) {
      //listen to the value when changed
    });
    super.initState();
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Center(
        child: Column(
          children: <Widget>[
            Padding(
                padding: const EdgeInsets.all(8.0),
                child: Center(
                  child: StateBlocBuilder<int>(
                    stateBloc: stateBloc,
                    widget: (int value) {
                      return Text("$value");
                    },
                  ),
                )),
            MaterialButton(
              color: Theme.of(context).primaryColor,
              child: Text(
                'Add',
                style: TextStyle(color: Colors.white),
              ),
              onPressed: () => stateBloc.changeState(state: (int value) {
                ++value;
                return value;
              }),
            ),
          ],
        ),
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  state_bloc: ^1.0.4

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:state_bloc/state_bloc.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
36
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]
68
Learn more about scoring.

We analyzed this package on Jul 8, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package not compatible with SDK dart

Because:

  • state_bloc that is a package requiring null.

Health suggestions

Format lib/state_bloc.dart.

Run flutter format to format lib/state_bloc.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test