Block Family
Block Family
▲▼▲▼▲▼
tags : #coding #flutter
references : BLoC
▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼▲▼
▲▼▲▼▲▼
What is Bloc?
Bloc (Business Logic Component) is a state management pattern in Flutter. It helps separate
business logic from UI code, making your app more maintainable and testable. Instead of
having the UI directly control how the data changes, Bloc acts like a middleman. You send an
event to the Bloc, and the Bloc handles the logic and sends back a new state.
The Bloc family is basically a group of tools that all work together to make this happen.
BlocProvider(
create: (context) => TransactionCubit(),
child: TransactionScreen(),
);
Here, TransactionCubit is the Bloc, and TransactionScreen will have access to it.
2. Bloc
What is it?
A Bloc is the brain of your app. It receives events and processes them to produce new
states. It doesn't have any UI logic; it only deals with the business logic.
How it works?
You create a Bloc class (like TransactionCubit ) that extends Cubit or Bloc . This
class defines states and events and provides logic for how to update the state when an
event happens. Example:
3. BlocBuilder
What is it?
BlocBuilder is like the watcher. It listens for changes in the state of your Bloc and
rebuilds your UI whenever the state changes. If the Bloc sends a new state,
BlocBuilder will automatically rebuild the widget based on that state.
How it works?
You use BlocBuilder to build the UI based on the current state of the Bloc. It listens
for state changes and rebuilds the widget when necessary. Example:
BlocBuilder<TransactionCubit, TransactionState>(
builder: (context, state) {
if (state is TransactionAddedState) {
return Text('Transaction Added');
}
return CircularProgressIndicator();
},
);
4. BlocListener
What is it?
BlocListener is like the reactor. It listens for state changes, but instead of rebuilding
the UI, it can perform side effects (like showing a dialog, navigating to a new screen,
etc.). It listens to events that need to trigger an action.
How it works?
You use BlocListener when you want to react to state changes but don’t need to
rebuild the UI. Example:
BlocListener<TransactionCubit, TransactionState>(
listener: (context, state) {
if (state is TransactionAddedState) {
// Show a success message or navigate to another screen
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content:
Text('Transaction Added')));
}
},
child: Container(),
);
5. BlocConsumer
What is it?
BlocConsumer is a combination of BlocBuilder and BlocListener . It lets you listen
to state changes and build the UI in the same widget. If you need both logic (side
effects) and UI updates in one place, BlocConsumer is your go-to.
How it works?
It provides both a listener and a builder in one widget, making it a powerful tool for
handling state changes. Example:
BlocConsumer<TransactionCubit, TransactionState>(
listener: (context, state) {
if (state is TransactionAddedState) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content:
Text('Transaction Added')));
}
},
builder: (context, state) {
if (state is TransactionAddedState) {
return Text('Transaction Added');
}
return CircularProgressIndicator();
},
);
How They All Work Together
6. BlocProvider provides the Bloc.
7. Bloc listens for events and handles the business logic.
8. BlocBuilder listens for state changes and updates the UI accordingly.
9. BlocListener listens for state changes to trigger side effects (e.g., navigation, showing
alerts).
10. BlocConsumer combines BlocBuilder and BlocListener when you need both UI
updates and side effects in one place.
Example in Practice:
Let's say you have a button that adds a transaction:
11. User clicks the button → This triggers an event in your Bloc (e.g., addTransaction ).
12. The Bloc receives the event and processes the logic.
13. The Bloc emits a new state (e.g., TransactionAddedState ).
14. BlocBuilder listens for the state and updates the UI (e.g., show "Transaction Added").
15. BlocListener reacts to the state change by showing a SnackBar with a success message.
Conclusion
So, BlocProvider sets up your Bloc, the Bloc manages the state, BlocBuilder updates the UI,
and BlocListener does side effects. BlocConsumer brings the best of both worlds—UI
updates and side effects—into one widget.
Sure! Here's a big table that highlights the differences between the various Bloc components: