bloc_pattern 1.5.2

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

Bloc Pattern #

Donate

Provider to implement Bloc Pattern in your Flutter code

Start #

Add bloc_pattern in your pubspec.yaml.

Create a Controller Bloc by implementing BlocBase and add its streams. OBS: You can pass the "context" in the Bloc.

import 'dart:async';
import 'package:bloc_pattern/bloc_pattern.dart';
import 'package:rxdart/rxdart.dart';

class BlocController implements BlocBase {

BlocController();

//Stream that receives a number and changes the count;
var _counterController = BehaviorSubject<int>(seedValue: 0);
//output
Stream<int> get outCounter => _counterController.stream;
//input
Sink<int> get inCounter => _counterController.sink;

increment(){
    inCounter.add(_counterController.value+1);
}



@override
void dispose() {
    _counterController.close();
}

}

Add the Provider in the main widget of your widget tree by passing as your BlocController parameter


...

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return BlocProvider<BlocController>(
      child: MaterialApp(
        home: MyHomePage(),
      ),
      bloc: BlocController(),
    );
  }
}

...

Now you can recover your Bloc anywhere in your widget tree with the help of BlocProvider


@override
  Widget build(BuildContext context) {
    //recovering your Bloc
  final BlocController bloc = BlocProvider.of<BlocController>(context);

  ....


}

Now just use StreamBuilder to get your streams and change the UI without needing setState


StreamBuilder(
    stream: bloc.outCounter,
    builder: (BuildContext context, AsyncSnapshot snapshot) {
    return Text(
        '${snapshot.data}',
        style: Theme.of(context).textTheme.display1,
    );
    },
),

  ....

floatingActionButton: new FloatingActionButton(
    onPressed: bloc.increment,
    tooltip: 'Increment',
    child: new Icon(Icons.add),
), 


}

Bloc Pattern With List #

Provider to implement a List Bloc Pattern in your Flutter code

Start #

Add bloc_pattern in your pubspec.yaml.

Create one or more Controller Bloc by implementing BlocBase and add its streams.

import 'dart:async';
import 'package:bloc_pattern/bloc_pattern.dart';
import 'package:rxdart/rxdart.dart';

class BlocControllerOne implements BlocBase {
   BlocControllerOne();

   //Stream that receives a number and changes the count;
   var _counterController = BehaviorSubject<int>(seedValue: 0);
   //output
   Stream<int> get outCounter => _counterController.stream;
   //input
   Sink<int> get inCounter => _counterController.sink;

   increment(){
      inCounter.add(_counterController.value+1);
   }

   @override
   void dispose() {
      _counterController.close();
   }

}

class BlocControllerTwo implements BlocBase {
   BlocControllerTwo();

   //Stream that receives a number and changes the count;
   var _counterController = BehaviorSubject<int>(seedValue: 0);
   //output
   Stream<int> get outCounter => _counterController.stream;
   //input
   Sink<int> get inCounter => _counterController.sink;

   increment(){
      inCounter.add(_counterController.value+1);
   }

   @override
   void dispose() {
      _counterController.close();
   }

}

Add the BlocProviderList in the main widget of your widget tree by passing as your BlocController parameter list


...

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return BlocProviderList(
      child: MaterialApp(
        home: IncrementWidget(),
      ),
      listBloc: [
        Bloc(BlocControllerOne()),
        Bloc(BlocControllerTwo())
      ],
    );
  }
}

...

Now you can recover yours Blocs anywhere in your widget tree with the help of BlocProviderList


@override
  Widget build(BuildContext context) {
    //recovering yours Blocs
  final BlocControllerOne blocOne = BlocProviderList.of<BlocControllerOne>(context);
  final BlocControllerTwo blocTwo = BlocProviderList.of<BlocControllerTwo>(context);

  ....

}

Para mais informações #

Acesse o Blog do Flutterando Clicando aqui.

Changelog #

1.1.2(8 de Dezembro 2018) #

  • removida injeção de contexto

1.1.1(8 de Dezembro 2018) #

  • removida injeção de contexto

1.1.0(19 de Novembro 2018) #

  • Injetado o context (BuildContext) no controller.

1.0.0(19 de Novembro 2018) #

  • Suporte ao Dart 2.1
  • Ciclo de vida do Bloc aprimorado com acesso rápido em qualquer lugar do aplicativo.

0.1.0 (8 de Novembro 2018) #

  • Provider com StatefulWidget.

example/README.md

Bloc Pattern #

Provider to implement Bloc Pattern in your Flutter code

Start #

Add bloc_pattern in your pubspec.yaml.

Create a Controller Bloc by implementing BlocBase and add its streams. OBS: You can pass the "context" in the Bloc.

import 'dart:async';
import 'package:bloc_pattern/bloc_pattern.dart';
import 'package:rxdart/rxdart.dart';

class BlocController implements BlocBase {

BlocController();

//Stream that receives a number and changes the count;
var _counterController = BehaviorSubject<int>(seedValue: 0);
//output
Stream<int> get outCounter => _counterController.stream;
//input
Sink<int> get inCounter => _counterController.sink;

increment(){
    inCounter.add(_counterController.value+1);
}

@override
void dispose() {
    _counterController.close();
}

}

Add the Provider in the main widget of your widget tree by passing as your BlocController parameter


...

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return BlocProvider<BlocController>(
      child: MaterialApp(
        home: MyHomePage(),
      ),
      bloc: BlocController(),
    );
  }
}

...

Now you can recover your Bloc anywhere in your widget tree with the help of BlocProvider


@override
  Widget build(BuildContext context) {
    //recuperando seu Bloc
  final BlocController bloc = BlocProvider.of<BlocController>(context);

  ....


}

Now just use StreamBuilder to get your streams and change the UI without needing setState


StreamBuilder(
    stream: bloc.outCounter,
    builder: (BuildContext context, AsyncSnapshot snapshot) {
    return Text(
        '${snapshot.data}',
        style: Theme.of(context).textTheme.display1,
    );
    },
),

  ....

floatingActionButton: new FloatingActionButton(
    onPressed: bloc.increment,
    tooltip: 'Increment',
    child: new Icon(Icons.add),
), 


}

Para mais informações #

Acesse o Blog do Flutterando Clicando aqui.

Use this package as a library

1. Depend on it

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


dependencies:
  bloc_pattern: ^1.5.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:bloc_pattern/bloc_pattern.dart';
  
Version Uploaded Documentation Archive
2.1.9+3 May 14, 2019 Go to the documentation of bloc_pattern 2.1.9+3 Download bloc_pattern 2.1.9+3 archive
2.1.9+2 May 13, 2019 Go to the documentation of bloc_pattern 2.1.9+2 Download bloc_pattern 2.1.9+2 archive
2.1.9+1 May 12, 2019 Go to the documentation of bloc_pattern 2.1.9+1 Download bloc_pattern 2.1.9+1 archive
2.1.9 May 11, 2019 Go to the documentation of bloc_pattern 2.1.9 Download bloc_pattern 2.1.9 archive
2.1.8 May 11, 2019 Go to the documentation of bloc_pattern 2.1.8 Download bloc_pattern 2.1.8 archive
2.1.7 May 9, 2019 Go to the documentation of bloc_pattern 2.1.7 Download bloc_pattern 2.1.7 archive
2.1.6 May 9, 2019 Go to the documentation of bloc_pattern 2.1.6 Download bloc_pattern 2.1.6 archive
2.1.0 May 2, 2019 Go to the documentation of bloc_pattern 2.1.0 Download bloc_pattern 2.1.0 archive
2.0.0 May 1, 2019 Go to the documentation of bloc_pattern 2.0.0 Download bloc_pattern 2.0.0 archive
1.5.2 Feb 18, 2019 Go to the documentation of bloc_pattern 1.5.2 Download bloc_pattern 1.5.2 archive

All 21 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
94
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]
97
Learn more about scoring.

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

  • Dart: 2.3.0
  • pana: 0.12.15
  • Flutter: 1.5.8

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health suggestions

Format lib/bloc_pattern.dart.

Run flutter format to format lib/bloc_pattern.dart.

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