Giter Site home page Giter Site logo

hoc081098 / flutter_bloc_pattern Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 2.0 188 KB

:zap: Base class, bloc provider and rxdart stream builder for BLoC pattern in flutter: https://pub.dev/packages/flutter_bloc_pattern

Home Page: https://pub.dev/packages/flutter_bloc_pattern

License: MIT License

Dart 94.31% Kotlin 0.33% Swift 1.11% Objective-C 0.10% HTML 4.15%
flutter-bloc-pattern flutter-bloc flutter-bloc-rxdart flutter-rxdart flutter-rx flutter-reactive bloc-pattern bloc-pattern-rxdart bloc-pattern-helper flutter-bloc-pattern-rxdart

flutter_bloc_pattern's Introduction

flutter_bloc_pattern

Base class, BLoC provider and rxdart builder for BLoC pattern in Flutter.

Flutter test Pub Pub codecov GitHub Style

Getting Started

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

dependencies:
  flutter_bloc_pattern: <latest_version>

2. Now in your Dart code, you can use:

import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart';

3. Implements BaseBloc:

class MyBloc implements BaseBloc {
  Stream<String> get stream;

  @override
  void dispose() {}
}

4. Consume BLoC:

 final bloc = BlocProvider.of<MyBloc>(context);
 return RxStreamBuilder(
  stream: bloc.stream,
  builder: (context, data) {
    return ...;
  },
);

Example: A port of the standard "Counter Button" example from Flutter

1. File counter_bloc.dart:

import 'dart:async';

import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart';
import 'package:rxdart_ext/rxdart_ext.dart';

class CounterBloc extends DisposeCallbackBaseBloc {
  /// Inputs
  final VoidAction increment;

  /// Outputs
  final StateStream<int> state;

  CounterBloc._({
    required void Function() dispose,
    required this.increment,
    required this.state,
  }) : super(dispose);

  factory CounterBloc() {
    // ignore: close_sinks
    final incrementController = StreamController<void>();

    final state = incrementController.stream
        .scan<int>((acc, _, __) => acc + 1, 0)
        .publishState(0);
    final connection = state.connect();

    return CounterBloc._(
      dispose: () async {
        await connection.cancel();
        await incrementController.close();
        print('CounterBloc::disposed');
      },
      increment: () => incrementController.add(null),
      state: state,
    );
  }
}

2. File main.dart:

import 'package:example/bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc_pattern/flutter_bloc_pattern.dart';

class TextCounter1 extends StatelessWidget {
  const TextCounter1({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final bloc = BlocProvider.of<CounterBloc>(context);

    return RxStreamBuilder<int>(
      stream: bloc.state,
      builder: (context, state) {
        return Text(
          'COUNTER 1: $state',
          style: Theme.of(context).textTheme.headline6,
        );
      },
    );
  }
}

class IncrementButton extends StatelessWidget {
  const IncrementButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final bloc = context.bloc<CounterBloc>();

    return FloatingActionButton(
      onPressed: bloc.increment,
      tooltip: 'Increment',
      child: Icon(Icons.add),
    );
  }
}

flutter_bloc_pattern's People

Contributors

hoc081098 avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

flutter_bloc_pattern's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update codecov/codecov-action action to v4

Edited/Blocked

These updates have been manually edited so Renovate will no longer make changes. To discard all commits and start over, click on a checkbox.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/flutter-test.yml
  • actions/checkout v3
  • subosito/flutter-action v2.6.1
  • codecov/codecov-action v3.1.0
gradle
example/android/gradle.properties
example/android/settings.gradle
example/android/build.gradle
  • com.android.tools.build:gradle 4.1.0
  • org.jetbrains.kotlin:kotlin-gradle-plugin 1.3.50
  • org.jetbrains.kotlin:kotlin-stdlib-jdk7 1.3.50
example/android/app/build.gradle
gradle-wrapper
example/android/gradle/wrapper/gradle-wrapper.properties
  • gradle 6.7
pub
example/pubspec.yaml
  • rxdart_ext ^0.2.2
  • flutter
  • cupertino_icons ^1.0.4
  • flutter_provider ^2.1.0
  • flutter_lints ^2.0.1
  • dart >=2.17.1 <3.0.0
  • flutter >=3.0.1
pubspec.yaml
  • flutter
  • rxdart_ext ^0.2.2
  • flutter_provider ^2.1.0
  • mockito ^5.0.16
  • build_runner ^2.1.2
  • flutter_lints ^1.0.4
  • dart >=2.14.0 <3.0.0
  • flutter >=2.5.0

  • Check this box to trigger a request for Renovate to run again on this repository

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: Cannot find preset's package (github>whitesource/merge-confidence:beta)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.