Skip to content

santoshakil/pattern_m

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

44 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Pattern M - Modular Flutter Architecture

A production-ready Flutter application template featuring a modular, scalable architecture with comprehensive tooling, clean architecture principles, and enterprise-grade features.

πŸ— Architecture Overview

Pattern M implements a feature-based clean architecture with clear separation of concerns, making it ideal for large-scale applications that require maintainability, testability, and team collaboration.

✨ Key Features

  • Clean Architecture: Strict separation between data, domain, and presentation layers
  • Feature-First Structure: Modular organization by features for better scalability
  • Advanced Dependency Injection: Centralized DI container with provider overrides
  • Multi-Environment Support: Built-in configurations for dev, staging, and production
  • Comprehensive Error Handling: Type-safe failures using Freezed with pattern matching
  • Enterprise Logging: Structured logging with environment-aware configurations
  • Advanced Routing: Protected routes, shell routes, and auth-aware navigation
  • Type-Safe State Management: Riverpod with code generation and compile-time safety
  • Rich Context Extensions: Productivity helpers for common UI operations
  • Internationalization Ready: Full i18n support with ARB files and type-safe access

πŸ“ Project Structure

lib/
β”œβ”€β”€ main.dart                    # App entry with DI and environment setup
β”œβ”€β”€ app.v.dart                   # Root widget with global configurations
β”œβ”€β”€ core/                        # Core functionality and infrastructure
β”‚   β”œβ”€β”€ config/
β”‚   β”‚   └── env.dart            # Environment configuration and feature flags
β”‚   β”œβ”€β”€ constants/              # App-wide constants and enums
β”‚   β”œβ”€β”€ di/
β”‚   β”‚   └── injection.dart      # Dependency injection container
β”‚   β”œβ”€β”€ errors/
β”‚   β”‚   β”œβ”€β”€ exceptions.dart     # Custom exceptions
β”‚   β”‚   β”œβ”€β”€ failures.dart       # Freezed failure types
β”‚   β”‚   └── failures.freezed.dart
β”‚   β”œβ”€β”€ extensions/             # Dart/Flutter extensions
β”‚   β”‚   β”œβ”€β”€ collection_extensions.dart
β”‚   β”‚   β”œβ”€β”€ context_extensions.dart    # UI helpers (theme, navigation, dialogs)
β”‚   β”‚   β”œβ”€β”€ datetime_extensions.dart
β”‚   β”‚   β”œβ”€β”€ string_extensions.dart
β”‚   β”‚   └── widget_extensions.dart
β”‚   β”œβ”€β”€ logger/
β”‚   β”‚   └── logger.s.dart       # Structured logging service
β”‚   β”œβ”€β”€ theme/
β”‚   β”‚   └── app_theme.dart      # Centralized theming
β”‚   └── utils/
β”‚       β”œβ”€β”€ formatters.dart     # Data formatting utilities
β”‚       └── validators.dart     # Input validation logic
β”œβ”€β”€ features/                   # Feature modules (clean architecture)
β”‚   β”œβ”€β”€ auth/                   # Authentication feature
β”‚   β”‚   β”œβ”€β”€ data/
β”‚   β”‚   β”‚   β”œβ”€β”€ datasources/    # Remote/local data sources
β”‚   β”‚   β”‚   β”œβ”€β”€ models/         # DTOs and serialization
β”‚   β”‚   β”‚   └── repositories/   # Repository implementations
β”‚   β”‚   β”œβ”€β”€ domain/
β”‚   β”‚   β”‚   β”œβ”€β”€ entities/       # Business models
β”‚   β”‚   β”‚   β”œβ”€β”€ repositories/   # Repository contracts
β”‚   β”‚   β”‚   └── usecases/       # Business logic
β”‚   β”‚   └── presentation/
β”‚   β”‚       β”œβ”€β”€ providers/      # State management (.p.dart)
β”‚   β”‚       β”œβ”€β”€ screens/        # Page widgets (.v.dart)
β”‚   β”‚       └── widgets/        # Feature-specific widgets
β”‚   β”œβ”€β”€ home/                   # Home feature
β”‚   └── settings/               # Settings feature
β”œβ”€β”€ l10n/                       # Localization
β”‚   β”œβ”€β”€ app_en.arb             # English translations
β”‚   β”œβ”€β”€ app_localizations.dart  # Generated
β”‚   └── app_localizations_en.dart
└── shared/                     # Cross-feature shared code
    β”œβ”€β”€ data/
    β”‚   └── services/
    β”‚       └── storage.s.dart  # Shared storage service
    β”œβ”€β”€ domain/
    β”‚   └── models/             # Shared domain models
    └── presentation/
        β”œβ”€β”€ providers/          # Global providers
        β”‚   β”œβ”€β”€ auth.p.dart     # Auth state provider
        β”‚   β”œβ”€β”€ router.p.dart   # Router with guards
        β”‚   └── theme.p.dart    # Theme state
        └── widgets/            # Shared UI components
            β”œβ”€β”€ error_screen.dart
            └── splash_screen.dart

🎯 File Naming Conventions

Pattern M uses specific file suffixes for clarity and consistency:

  • .v.dart - Views/Screens: UI components that represent full pages
  • .p.dart - Providers: Riverpod state management files
  • .s.dart - Services: Business logic services (storage, API, etc.)
  • .g.dart - Generated: Auto-generated files (do not edit)
  • .freezed.dart - Freezed: Generated immutable models

πŸš€ Getting Started

Prerequisites

  • Flutter SDK ^3.8.1
  • Dart SDK (included with Flutter)
  • Android Studio / VS Code with Flutter extensions

Initial Setup

  1. Clone the repository

    git clone <repository-url>
    cd pattern_m
  2. Install dependencies

    flutter pub get
  3. Generate code

    dart run build_runner build --delete-conflicting-outputs
  4. Run the application

    flutter run

πŸ’» Development

Code Generation

The project heavily uses code generation for boilerplate reduction:

# One-time generation
dart run build_runner build --delete-conflicting-outputs

# Watch mode for development (recommended)
dart run build_runner watch --delete-conflicting-outputs

Generated files include:

  • Riverpod providers (*.p.g.dart)
  • Freezed models (*.freezed.dart, *.g.dart)
  • JSON serialization (*.g.dart)
  • Localization (app_localizations*.dart)

Environment Configuration

The app supports multiple environments configured in lib/core/config/env.dart:

// Automatic environment detection
- Debug mode β†’ Development
- Release mode β†’ Production
- Environment variable β†’ Staging

// Environment-specific features
- API endpoints
- Feature flags
- Timeouts
- Analytics/Crash reporting

Adding New Features

  1. Create feature structure

    lib/features/new_feature/
    β”œβ”€β”€ data/
    β”‚   β”œβ”€β”€ datasources/
    β”‚   β”œβ”€β”€ models/
    β”‚   └── repositories/
    β”œβ”€β”€ domain/
    β”‚   β”œβ”€β”€ entities/
    β”‚   β”œβ”€β”€ repositories/
    β”‚   └── usecases/
    └── presentation/
        β”œβ”€β”€ providers/
        β”œβ”€β”€ screens/
        └── widgets/
    
  2. Implement layers bottom-up

    • Start with domain entities and repositories
    • Implement data layer with concrete repositories
    • Create providers for state management
    • Build UI screens and widgets
  3. Register in DI container (if needed)

    // In lib/core/di/injection.dart
    static Future<void> init() async {
      // Add your service initialization
    }
  4. Add routes

    // In lib/shared/presentation/providers/router.p.dart
    GoRoute(
      path: '/new-feature',
      name: 'newFeature',
      builder: (context, state) => const NewFeatureScreen(),
    ),

State Management Patterns

Provider with Code Generation

@riverpod
class FeatureName extends _$FeatureName {
  @override
  FeatureState build() => FeatureState.initial();
  
  void updateState() {
    state = state.copyWith(/* updates */);
  }
}

Async Provider

@riverpod
Future<List<Item>> fetchItems(Ref ref) async {
  final repository = ref.watch(itemRepositoryProvider);
  return repository.getItems();
}

Error Handling

Pattern M uses type-safe error handling with Freezed:

// Define failures
@freezed
class Failure with _$Failure {
  const factory Failure.server({
    required String message,
    int? statusCode,
  }) = ServerFailure;
  
  const factory Failure.network({
    required String message,
  }) = NetworkFailure;
}

// Handle failures
failure.when(
  server: (message, code) => showServerError(message),
  network: (message) => showNetworkError(message),
  // ... other cases
);

Using Context Extensions

The framework provides rich context extensions for common operations:

// Theme access
context.primaryColor
context.textTheme.headlineLarge

// Responsive design
if (context.isMobile) // < 600px
if (context.isTablet) // 600-1200px
if (context.isDesktop) // > 1200px

// Navigation helpers
context.showErrorSnackBar('Error message');
context.showSuccessSnackBar('Success!');
context.showLoadingDialog();
context.showAppBottomSheet(child: MySheet());

// Localization
context.l10n.welcomeMessage

πŸ§ͺ Testing

Unit Tests

flutter test

# With coverage
flutter test --coverage

# Generate coverage report
genhtml coverage/lcov.info -o coverage/html

Test Structure

test/
β”œβ”€β”€ core/
β”‚   β”œβ”€β”€ utils/
β”‚   └── extensions/
β”œβ”€β”€ features/
β”‚   β”œβ”€β”€ auth/
β”‚   β”‚   β”œβ”€β”€ domain/
β”‚   β”‚   β”œβ”€β”€ data/
β”‚   β”‚   └── presentation/
β”‚   └── home/
└── shared/

Testing Best Practices

  • Test use cases with mocked repositories
  • Test providers with ProviderContainer
  • Use mocktail or mockito for mocking
  • Focus on business logic coverage

πŸ” Security Considerations

  • Never commit sensitive data (API keys, secrets)
  • Use environment variables for configuration
  • Implement certificate pinning for production
  • Enable obfuscation for release builds
  • Regular dependency updates

πŸ“± Platform-Specific Setup

Android

  • Minimum SDK: 21 (Android 5.0)
  • Target SDK: Latest stable
  • ProGuard rules for release builds

iOS

  • Minimum deployment target: 12.0
  • Swift version: Latest stable
  • Code signing for distribution

Web

  • Responsive design support
  • PWA capabilities
  • SEO optimization ready

πŸ— Build & Deployment

Development Build

flutter run --debug

Staging Build

flutter run --dart-define=ENVIRONMENT=staging

Production Builds

# Android
flutter build apk --release --obfuscate --split-debug-info=debug_info

# iOS
flutter build ios --release --obfuscate --split-debug-info=debug_info

# Web
flutter build web --release --web-renderer canvaskit

πŸ”§ Troubleshooting

Common Issues

  1. Build runner conflicts

    dart run build_runner clean
    dart run build_runner build --delete-conflicting-outputs
  2. Pod install failures (iOS)

    cd ios
    pod deintegrate
    pod install --repo-update
  3. Gradle issues (Android)

    cd android
    ./gradlew clean
    ./gradlew build

πŸ“š Best Practices

  1. Architecture

    • Keep layers independent
    • Depend on abstractions, not concretions
    • One feature, one folder
    • Shared code in /shared
  2. State Management

    • Use code generation for type safety
    • Keep providers focused and testable
    • Dispose resources properly
  3. Error Handling

    • Always handle failures explicitly
    • Provide user-friendly error messages
    • Log errors for debugging
  4. Performance

    • Use const constructors
    • Implement proper list optimization
    • Profile before optimizing
    • Monitor app size

🀝 Contributing

  1. Follow the established architecture
  2. Write tests for new features
  3. Update documentation
  4. Run linting: flutter analyze
  5. Format code: dart format .
  6. Create detailed pull requests

πŸ“„ License

This project is a template and can be used freely for any purpose.

About

This is a Flutter project template.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published