This Flutter project serves as a practical demonstration of state management using the Riverpod package. It's a simple shopping cart application where users can view a list of products, add them to a cart, and see the total cost.
The primary goal of this project is to showcase a clean and scalable architecture for managing state in a Flutter application, leveraging Riverpod's code generation capabilities for simplicity and robustness.
- Product Catalog: Displays a list of available products with their names, prices, and images.
- Shopping Cart: Allows users to add and remove products from their cart.
- Live Cart Total: The total price of all items in the cart is calculated and displayed in real-time.
- State Persistence (Conceptual): The architecture is set up to easily add state persistence if needed.
This project heavily relies on the flutter_riverpod package, along with riverpod_annotation and riverpod_generator, to manage the application's state. This modern approach simplifies provider creation and ensures type safety.
The core of the state management is organized into providers:
productsProvider: A simpleProviderthat exposes the complete list of available products. The data is currently hardcoded for demonstration purposes.reducedProductsProvider: AProviderthat demonstrates business logic by exposing a filtered list of products (e.g., those with a price less than 50).
cartNotifierProvider: ANotifierProviderthat exposes aCartNotifier. This notifier contains the logic for managing the shopping cart.addProduct(Product product): Adds a given product to the cart.removeProduct(Product product): Removes a product from the cart.- The state of the notifier is a
Set<Product>to automatically handle duplicate entries.
cartTotalProvider: AProviderthat "watches" thecartNotifierProviderand calculates the total price of all products currently in the cart. This is a great example of a computed provider, which automatically recalculates when its dependencies change.
The lib directory is organized to maintain a clean and scalable codebase:
lib/
├── models/
│ └── product.dart # The data model for a Product.
├── providers/
│ ├── cart_provider.dart # Providers for cart state and logic.
│ └── product_provider.dart # Providers for product data.
├── screens/
│ ├── cart/ # Widgets and UI for the cart screen.
│ └── home/ # Widgets and UI for the home screen.
├── shared/
│ └── cart_icon.dart # A reusable widget for the app bar.
└── main.dart # The main entry point of the application.
flutter_riverpod: The core package for state management.riverpod_annotation: Provides the annotations (@riverpod) used for code generation.riverpod_generator: The code generator that creates the.g.dartfiles, reducing boilerplate.build_runner: The tool used to run code generators in Dart.
-
Clone the repository:
git clone <your-repo-url> cd state_management_flutter
-
Get dependencies:
flutter pub get
-
Run the code generator: This step is necessary to generate the provider files (
*.g.dart).dart run build_runner watch
Use
watchfor continuous regeneration as you code, orbuildfor a one-time build. -
Run the app:
flutter run
This README was generated to provide a clear and comprehensive overview of the project.