form_bloc icon indicating copy to clipboard operation
form_bloc copied to clipboard

Example of how to add additional states and events?

Open JohnGalt1717 opened this issue 3 years ago • 4 comments

I'm wanting to create a shared framework for our projects with flutter. We have 3 basic screens that I'm trying to not duplicate code for:

  1. Forms (covered by form_bloc)
  2. Lists/fitlers - These are pages that present the user with the option to create a new X item or edit an existing one.
  3. Dashboards

All 3 share the Unloaded, loading, loaded flow. They differ from there. Dashboards have a drilldown event, lists have an add and an edit event. I'd guess that most apps especially web follow a similar model. They may have additional ones for other interactions, but these will be the basics.

I'd love to come up with a model that allows me to extend form_bloc to add these other flows so that I can share the same logic for all of the shared steps/events and states.

So I was looking at how to extend the current form_bloc events and states but couldn't find anything by way of an example.

Suggestions?

JohnGalt1717 avatar Jul 21 '20 12:07 JohnGalt1717

Hello @JohnGalt1717, Interesting, you could be a little more descriptive of what kind of events and statesyou would like to add, so I can focus more.

I think it would be possible if I add a generic property and type ExtraData like in the FieldBlocs, in which you can add the Object (state) you want, (I don't know if that name is good haha).

And adding a method to emit custom events, this sounds to me to change the implementation to cubit in this way it is easier for everyone, because can use methods instead of events.

The implementation would review the following week, but if you have comments they would be helpful to better focus everything.


Related to https://github.com/GiancarloCode/form_bloc/issues/118 So @ayushin please check :)

GiancarloCode avatar Aug 03 '20 07:08 GiancarloCode

@GiancarloCode So here's a few:

Add/New (Adding a new object) Edit (edit a specific item based on id) But basically it would be nice to be able to add any custom event to a page so that we don't have to have a second bloc to manage other things that can happen on pages. An excellent example, is if you want to maintain state but load a dialog box or something and you could have many on a page.

JohnGalt1717 avatar Aug 03 '20 13:08 JohnGalt1717

I would say Add/Edit/View functionality is one of the most probably use-case for that. But it would definitely be beneficial for other scenarios to have access to add new events/states/mapping. Looking forward to this as well..

dinana avatar Aug 20 '20 08:08 dinana

I landed here after banging my head against the desk looking for a possible solution.

Issue:

  • Need either i) a way to add more Events / States or ii) an example on how to combine a FormBloc with anotherBloc while being able to listen to those events from FormBlocListener

I have three classes:

  • class MyFormBloc extends FormBloc<String, String> {}
  • class MyFormt extends StatefulWidget {} - all the TextFieldBlocBuilders live here
  • class AddEditItemBloc extends Bloc<AddEditItemEvent, AddEditItemState> {} - to control the databse events and Items list display.

Naive solution: The simplest way to make this work is to use MyFormBloc.submit() to call methods in another bloc, such as addEditItemBloc.addItem(myObject), which add events to that other bloc addEditItemBloc.

Additional Problem: I haven't been able to listen to AddEditItemBloc events from MyForm. I understand this should be done using FormBlocListener, but I can't find a way to add more events beside onSubmitting, onSuccess and onFailure:

FormBlocListener<MyFormBloc, String, String>(
    onSubmitting: (BuildContext context, FormBlocSubmitting<String, String> state) {},
    onSuccess: (BuildContext context, FormBlocSuccess<String, String> state) {},
    onFailure: (BuildContext context, FormBlocFailure<String, String> state) {},
)

Thank you for all the hard work on the FormBloc!

caleonardo avatar Sep 15 '20 21:09 caleonardo