What are all the common design patterns used in flutter projects ?
#flutter
Answer
Common Design Patterns in Flutter
Design patterns are reusable solutions to common problems.
Architectural Patterns
MVC (Model-View-Controller)
- Model: Data
- View: UI
- Controller: Logic
- Simple, good for small apps
MVP (Model-View-Presenter)
- Model: Data
- View: UI (passive)
- Presenter: Logic
- Better separation than MVC
MVVM (Model-View-ViewModel)
- Model: Data
- View: UI
- ViewModel: Logic with data binding
- Most popular in Flutter
Clean Architecture
- Domain, Data, Presentation layers
- Dependency injection
- Repository pattern
- Enterprise apps
Behavioral Patterns
Singleton
dartclass Logger { static final Logger _instance = Logger._internal(); Logger._internal(); factory Logger() => _instance; }
Observer
dartfinal valueNotifier = ValueNotifier<int>(0); valueNotifier.addListener(() => print('Changed'));
Builder
dartClass Custom { int a, b; Custom({required this.a, required this.b}); }
UI Patterns
- Widget composition: Build complex UIs from simple widgets
- State management: Provider, Riverpod, BLoC
- Navigation: Named routes, Go Router
- Responsive design: MediaQuery, LayoutBuilder
Best Practice: Choose patterns based on app complexity.