CCBloc is a package developed to simplify the usage of BLoC (Business Logic Component) in Flutter applications. This package helps you manage BLoC operations more efficiently and in a more organized manner. Below are the main features of the package and usage examples.
- Simple and centralized BLoC management
- Easy access to handle loading and update states
- Dynamic management of modules and providers
Add the following line to your pubspec.yaml
file to include the package in your project:
dependencies:
cc_bloc: ^1.0.0
Then, run flutter pub get
in the terminal to install the package.
The CCBloc
class allows you to manage and access your BLoCs.
To register a BLoC class:
CCBloc.register(MainCubit());
To access a registered BLoC:
var cubit = CCBloc.getModule<MainCubit>();
To handle loading and update states:
CCBloc.loading<MainCubit>();
CCBloc.updated<MainCubit>();
Create a BLoC class and register it with CCBloc:
class MainCubit extends Cubit<MainState> {
MainCubit() : super(MainInitial()) {
CCBloc.register(this);
}
void fetch() async {
CCBloc.loading<MainCubit>();
// Perform BLoC operations
CCBloc.updated<MainCubit>();
}
}
Create a widget and apply the CCProvider
mixin:
class MainPage extends CCStatelessWidget<MainCubit> {
MainPage({Key? key}) : super(key: key);
@override
void loading() {
print("Loading");
}
@override
void updated() {
print("Updated");
}
@override
Widget build(BuildContext context) {
return BlocBuilder<MainCubit, MainState>(
builder: (context, state) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('You have pushed the button this many times:'),
Text(
'2',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
);
},
);
}
}
If you'd like to contribute, please submit pull requests or report issues.
This package is licensed under the MIT License. See the LICENSE
file for details.