Refactor high-complexity React components in Dify frontend. Use when `pnpm analyze-component...
npx skills add AbdelhakRazi/flutter-bloc-clean-architecture-skill
Or install specific skill: npx add-skill https://github.com/AbdelhakRazi/flutter-bloc-clean-architecture-skill
# Description
Build Flutter features using BLoC state management, clean architecture layers, and the project's design system. Apply when creating screens, widgets, or data integrations.
# SKILL.md
name: flutter-bloc-development
description: Build Flutter features using BLoC state management, clean architecture layers, and the project's design system. Apply when creating screens, widgets, or data integrations.
license: Complete terms in LICENSE.txt
Flutter BLoC Development
This skill enforces BLoC state management, strict layer separation, and mandatory use of design system constants for all Flutter development in this codebase.
Decision Tree: Choosing Your Approach
User task β What are they building?
β
ββ New screen/feature β Full feature implementation:
β 1. Create feature folder (lib/[feature]/)
β 2. Define BLoC (bloc/[feature]_event.dart, _state.dart, _bloc.dart)
β 3. Create data layer (data/datasources/, data/repositories/, data/models/)
β 4. Build UI (view/[feature]_page.dart, view/widgets/)
β 5. Create barrel files ([feature].dart, data/data.dart, view/view.dart)
β
ββ New widget only β Presentation layer:
β 1. Feature-specific: feature/view/widgets/
β 2. Shared/reusable: shared/widgets/
β 3. Use design system constants (NO hardcoded values)
β 4. Connect to existing BLoC if needed
β
ββ Data integration β Data layer only:
β 1. Create datasource (feature/data/datasources/)
β 2. Create repository (feature/data/repositories/)
β 3. Wire up in existing or new BLoC
β
ββ Refactoring β Identify violations:
1. Check for hardcoded colors/spacing/typography
2. Check for business logic in UI
3. Check for direct SDK calls outside datasources
4. Check for missing Loading state before async operations
5. Check for missing Equatable on Events/States
6. Check for improper error handling (use SnackBar + AppColors.error)
Architecture at a Glance
Feature-first structure (official BLoC recommendation):
lib/
βββ [feature]/ # Feature folder (e.g., earnings/, auth/, trips/)
β βββ bloc/
β β βββ [feature]_bloc.dart
β β βββ [feature]_event.dart
β β βββ [feature]_state.dart
β βββ data/
β β βββ datasources/ # Feature-specific API calls
β β βββ repositories/ # Data orchestration
β β βββ models/ # Feature-specific DTOs
β β βββ data.dart # Data layer barrel file
β βββ view/
β β βββ [feature]_page.dart # Main screen
β β βββ widgets/ # Feature-specific widgets
β β βββ view.dart # View barrel file
β βββ [feature].dart # Feature barrel file
βββ shared/ # Cross-feature code
β βββ data/
β β βββ datasources/ # Shared API clients (ApiClient, UserDataSource)
β β βββ models/ # Shared models (User, ApiResponse)
β β βββ data.dart # Shared data barrel file
β βββ widgets/ # Reusable UI components
β βββ utils/ # Design system (colors, spacing, typography)
βββ app.dart # App entry point
When to Use Feature vs Shared Data
| Scenario | Location | Example |
|---|---|---|
| API endpoints used by ONE feature | feature/data/ |
EarningsDataSource β /api/earnings/... |
| API client/service used by MANY features | shared/data/ |
ApiClient, UserDataSource |
| Models used by ONE feature | feature/data/models/ |
EarningsSummary |
| Models used by MANY features | shared/data/models/ |
User, ApiResponse |
Barrel Files β Single import per layer:
// Feature barrel: earnings/earnings.dart
export 'bloc/earnings_bloc.dart';
export 'bloc/earnings_event.dart';
export 'bloc/earnings_state.dart';
export 'data/data.dart';
export 'view/view.dart';
// Data layer barrel: earnings/data/data.dart
export 'datasources/earnings_datasource.dart';
export 'repositories/earnings_repository.dart';
export 'models/earnings_summary.dart';
// Shared data barrel: shared/data/data.dart
export 'datasources/api_client.dart';
export 'datasources/user_datasource.dart';
export 'models/user.dart';
Key Rules:
- All state changes flow through BLoC
- No direct backend SDK calls outside datasources
- Zero hardcoded values (colors, spacing, typography)
- Repository pattern for all data access
- Feature-specific code stays in feature folder
- Shared code (used by 2+ features) goes in shared/
BLoC Implementation
Event β State β BLoC (Three Files Per Feature)
Events β User actions and system triggers:
abstract class FeatureEvent extends Equatable {
const FeatureEvent();
@override
List<Object?> get props => [];
}
class FeatureActionRequested extends FeatureEvent {
final String param;
const FeatureActionRequested({required this.param});
@override
List<Object> get props => [param];
}
States β All possible UI states:
abstract class FeatureState extends Equatable {
const FeatureState();
@override
List<Object?> get props => [];
}
class FeatureInitial extends FeatureState {}
class FeatureLoading extends FeatureState {}
class FeatureSuccess extends FeatureState {
final DataType data;
const FeatureSuccess(this.data);
@override
List<Object> get props => [data];
}
class FeatureError extends FeatureState {
final String message;
const FeatureError(this.message);
@override
List<Object> get props => [message];
}
BLoC β Event handlers with Loading β Success/Error pattern:
class FeatureBloc extends Bloc<FeatureEvent, FeatureState> {
final FeatureRepository _repository;
FeatureBloc({required FeatureRepository repository})
: _repository = repository,
super(FeatureInitial()) {
on<FeatureActionRequested>(_onActionRequested);
}
Future<void> _onActionRequested(
FeatureActionRequested event,
Emitter<FeatureState> emit,
) async {
emit(FeatureLoading());
try {
final result = await _repository.doSomething(event.param);
emit(FeatureSuccess(result));
} catch (e) {
emit(FeatureError(e.toString()));
}
}
}
CRITICAL: Always emit Loading before async work, then Success or Error. Never skip the loading state.
Data Layer
Data Flow:
UI Event β BLoC (emit Loading) β Repository β Datasource (SDK)
β
Response β Repository (map to entity) β BLoC (emit Success/Error) β UI
Datasource β Backend SDK calls only:
class FeatureDataSource {
final SupabaseClient _supabase;
FeatureDataSource(this._supabase);
Future<Map<String, dynamic>> fetch() async {
return await _supabase.from('table').select().single();
}
}
Repository β Orchestration and mapping:
class FeatureRepository {
final FeatureDataSource _dataSource;
FeatureRepository(this._dataSource);
Future<DomainEntity> fetchData() async {
final response = await _dataSource.fetch();
return DomainEntity.fromJson(response);
}
}
Design System (Non-Negotiable)
Colors
β
AppColors.primary, AppColors.error, AppColors.textPrimary
β Color(0xFF...), Colors.blue, inline hex values
Spacing
β
AppSpacing.xs (4), AppSpacing.sm (8), AppSpacing.md (16), AppSpacing.lg (24), AppSpacing.xl (32)
β
AppSpacing.screenHorizontal (24), AppSpacing.screenVertical (16)
β EdgeInsets.all(16.0), hardcoded padding values
Border Radius
β
AppRadius.sm (8), AppRadius.md (12), AppRadius.lg (16), AppRadius.xl (24)
β BorderRadius.circular(12), inline radius values
Typography
β
AppTypography.headlineLarge, AppTypography.bodyMedium, theme.textTheme.bodyMedium
β TextStyle(fontSize: 16), inline text styles
UI Patterns
Screen Template
GradientScaffold(
body: SafeArea(
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(AppSpacing.screenHorizontal),
child: HeaderWidget(),
),
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: AppSpacing.screenHorizontal),
child: ContentWidget(),
),
),
Padding(
padding: const EdgeInsets.all(AppSpacing.screenHorizontal),
child: ActionButton(
onPressed: () => context.read<FeatureBloc>().add(ActionEvent()),
),
),
],
),
),
)
BLoC Consumer Pattern
BlocConsumer<FeatureBloc, FeatureState>(
listener: (context, state) {
if (state is FeatureError) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(state.message), backgroundColor: AppColors.error),
);
}
},
builder: (context, state) {
if (state is FeatureLoading) return const Center(child: CircularProgressIndicator());
if (state is FeatureSuccess) return SuccessWidget(data: state.data);
return const SizedBox.shrink();
},
)
Common Pitfalls
β Business logic in widgets β Move to BLoC
β Direct Supabase/Firebase calls in repository β Move to datasource
β Skipping loading state before async operations β Always emit Loading first
β Hardcoded colors like Color(0xFF4A90A4) β Use AppColors.primary
β Magic numbers like padding: 16 β Use AppSpacing.md
Quick Reference
| Action | Pattern |
|---|---|
| Dispatch event | context.read<Bloc>().add(Event()) |
| Watch state inline | context.watch<Bloc>().state |
| Listen + Build | BlocConsumer |
| Listen only | BlocListener |
| Build only | BlocBuilder |
Checklist Before Submitting
- [ ] Events/States/BLoC use
Equatable - [ ] All async: Loading β Success/Error
- [ ] No business logic in UI
- [ ] No SDK calls outside datasources
- [ ] Zero hardcoded colors/spacing/typography
- [ ] Error handling shows SnackBar with
AppColors.error - [ ] Code formatted with
dart format
# README.md
Flutter BLoC + Clean Architecture
A set of instructions to help your AI coding assistant build Flutter apps with proper architecture with BLoC state management, clean layers, and consistent design themes.
What's Inside
SKILL.md: The architecture rules and patternsexamples/: Working code samples
How It Works
UI β BLoC β Repository β Datasource β Backend
No business logic in widgets. No hardcoded colors. No magic numbers.
Get Started
Install with the Skills CLI:
npx skills add https://github.com/abdelhakrazi/flutter-bloc-clean-architecture-skill --skill flutter-bloc-development
Or grab this repo and add it to your AI coding assistant manually.
Examples
- earnings/: Full feature with feature-first structure (official BLoC pattern)
- shared/data/: Shared datasources and models used across features
- shared/widgets/: Reusable design-system-compliant widgets
License
Apache 2.0
# Supported AI Coding Agents
This skill is compatible with the SKILL.md standard and works with all major AI coding agents:
Learn more about the SKILL.md standard and how to use these skills with your preferred AI coding agent.