Guide for creating BLoC patterns with events, states, and proper structure in app_bloc packages (project)
This skill guides the creation of BLoC (Business Logic Component) packages following this project's conventions.
Trigger this skill when:
Always use Mason templates first:
# Simple BLoC (basic state management)
mason make simple_bloc -o app_bloc/feature_name --name=feature_name
# List BLoC (for managing lists with CRUD operations)
mason make list_bloc -o app_bloc/feature_name --name=feature_name
# Form BLoC (for form validation and submission)
mason make form_bloc --name Login --field_names "email,password"
BLoC packages MUST be created in app_bloc/:
app_bloc/
└── feature_name/
├── lib/
│ ├── feature_name_bloc.dart # Barrel export file
│ └── src/
│ ├── bloc.dart # BLoC class with event handlers
│ ├── event.dart # Event definitions
│ └── state.dart # State class with status enum
├── test/
│ └── feature_name_bloc_test.dart
└── pubspec.yaml
LoadData, SubmitForm, ChangeThemeDataLoaded, FormSubmittedinitial, loading, completed, errorcopyWith() for immutable state updatesEquatable for proper state comparison_on: _onLoadData, _onSubmitFormEmitter<State> for state emissionsThemeBloc (app_bloc/theme/) serves as the reference implementation:
class ThemeBloc extends Bloc<ThemeEvent, ThemeState> {
ThemeBloc(this.pref) : super(ThemeState.initial(pref)) {
on<ChangeTheme>(_onThemeChanged);
on<ChangeThemeMode>(_onThemeModeChanged);
}
Future<void> _onThemeChanged(
ChangeTheme event,
Emitter<ThemeState> emitter,
) async {
emitter(state.copyWith(theme: event.theme));
pref.setString('themeName', event.theme.name);
}
}
In pubspec.yaml, use workspace resolution: