Coder Social home page Coder Social logo

flutter-generic-bloc's Introduction

Simple Generic Bloc

It will help you implement Bloc by writing few dart file/code since it is Generic

Dependencies:

  • equatable
  • flutter_bloc

Usage:

  • add generic_bloc.dart in any path of your project's lib folder.

Create a Bloc object:

  • create a bloc dart file eg:counter_bloc.dart in your project.
  • extend class to SimpleGenericBloc and cast generic class within the object class you want to use for the bloc.
  • here we are using SimpleGenericBloc<int?> because our bloc will store only integer, you can put your model or any other object type.
//import generic_bloc.dart

import 'package:path/to/generic_bloc.dart';

// create event's

class Increment extends GenericEvent {}

// create bloc class

class CounterBloc extends SimpleGenericBloc<int?> {
int? count;

CounterBloc() : super(SimpleGenericState.initial(0)) {

  on<GenericEvent>((event, emit) async {
    if (event is Increment) {
      count++;
      emit(SimpleGenericState.update(count));
    }
  });
}
}
  • implement the bloc to your widget:
class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {

    //get the bloc using di or whatever way you use...
    //eg:
    final bloc = getIt<CounterBloc>();

    return BlocProvider(
        create: (context) => bloc,
        //make BlocBuilder with generic 
        child: BlocBuilder<CounterBloc, SimpleGenericState<int?>>(
          builder: (context, state) {

            

            return Column(
              children: [
                Text(
                  'You clicked ${state.value} times',
                ),
                TextButton(
                  onPressed: () {
                    //call event
                    bloc.add(Increment());
                  },
                  child: Text('Increment'),
                )
              ],
            );
          },
        ));
  }
}


flutter-generic-bloc's People

Contributors

abappi19 avatar

Watchers

 avatar

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.