How chinmaynkr/stock_avg_calculator works
Project type and purpose
• This is a Flutter-based, cross-platform mobile (Android, iOS), desktop (Windows, macOS, Linux), and web application.
• Purpose: Let users add stock purchase entries (quantity, unit price), compute total investment and overall average price, manage entries (delete, reset), and persist history locally using SharedPreferences.
File structure
• Top-level “lib/” holds the Dart source: main.dart, UI screens, models, providers.
• “android/”, “ios/”, “windows/”, “macos/”, “linux/”, “web/” directories contain platform-specific embedder code generated by Flutter.
• pubspec.yaml + pubspec.lock list dependencies, including provider (state management) and shared_preferences.
README for insights
• Architecture is not explicitly drawn but mentions Provider for state management and SharedPreferences for persistence.
• Features: Entry addition, deletion, reset, history, theming.
Main components and their interactions
a. UI Layer (Flutter Widgets)
• Screens/Widgets:
• EntryFormWidget (quantity & price input, “Add Entry” button)
• HistoryScreen (lists all past entries with timestamps)
• SettingsScreen (theme toggle, social links)
• Interacts with State Provider to read and modify data.
b. State Management Layer
• Provider(s): e.g., StockEntryProvider or HistoryProvider
• Holds in-memory list of entries, computes total and average.
• Exposes methods: addEntry(), deleteEntry(), resetEntries().
c. Persistence Layer
• SharedPreferences plugin (Flutter → native mobile storage)
• On each add/delete/reset, Provider writes updated history to SharedPreferences.
• At app startup, Provider loads persisted history.
d. Domain Model
• StockEntry: data class with quantity, pricePerUnit, totalInvestment, timestamp.
e. Flutter Engine & Platform Embedders
• Flutter framework and Dart runtime communicate via platform channels to native code for SharedPreferences and theming.
f. Third-Party Libraries
• provider: state management
• shared_preferences: persistence
• cupertino_icons, flutter_localizations (optional)
Architectural patterns & design principles
• Provider pattern (simple MVVM-style):
• View (Widgets) binds to ViewModel (Provider)
• ViewModel updates Model (StockEntry list) and notifies View
• Single responsibility:
• UI widgets focus on rendering, Provider on state, SharedPreferences on persistence.
• Dependency injection via Provider’s MultiProvider at root (in main.dart).
Detail level
• Every logical component: entry form, list widget, provider, model, persistence plugin, Flutter Engine, platform embedder.
• Avoid over-abstracting; break UI layer into main screens.
• Data flow from user input → in-memory state → persistence → UI update.