mirror of
https://github.com/flutter/samples.git
synced 2025-11-11 23:39:14 +00:00
115 lines
3.1 KiB
Dart
115 lines
3.1 KiB
Dart
// Copyright 2019 The Flutter team. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
import 'package:scoped_model/scoped_model.dart';
|
|
|
|
import 'package:gallery/studies/shrine/model/product.dart';
|
|
import 'package:gallery/studies/shrine/model/products_repository.dart';
|
|
|
|
double _salesTaxRate = 0.06;
|
|
double _shippingCostPerItem = 7;
|
|
|
|
class AppStateModel extends Model {
|
|
// All the available products.
|
|
List<Product> _availableProducts;
|
|
|
|
// The currently selected category of products.
|
|
Category _selectedCategory = categoryAll;
|
|
|
|
// The IDs and quantities of products currently in the cart.
|
|
final Map<int, int> _productsInCart = <int, int>{};
|
|
|
|
Map<int, int> get productsInCart => Map<int, int>.from(_productsInCart);
|
|
|
|
// Total number of items in the cart.
|
|
int get totalCartQuantity => _productsInCart.values.fold(0, (v, e) => v + e);
|
|
|
|
Category get selectedCategory => _selectedCategory;
|
|
|
|
// Totaled prices of the items in the cart.
|
|
double get subtotalCost {
|
|
return _productsInCart.keys
|
|
.map((id) => _availableProducts[id].price * _productsInCart[id])
|
|
.fold(0.0, (sum, e) => sum + e);
|
|
}
|
|
|
|
// Total shipping cost for the items in the cart.
|
|
double get shippingCost {
|
|
return _shippingCostPerItem *
|
|
_productsInCart.values.fold(0.0, (sum, e) => sum + e);
|
|
}
|
|
|
|
// Sales tax for the items in the cart
|
|
double get tax => subtotalCost * _salesTaxRate;
|
|
|
|
// Total cost to order everything in the cart.
|
|
double get totalCost => subtotalCost + shippingCost + tax;
|
|
|
|
// Returns a copy of the list of available products, filtered by category.
|
|
List<Product> getProducts() {
|
|
if (_availableProducts == null) {
|
|
return [];
|
|
}
|
|
|
|
if (_selectedCategory == categoryAll) {
|
|
return List<Product>.from(_availableProducts);
|
|
} else {
|
|
return _availableProducts
|
|
.where((p) => p.category == _selectedCategory)
|
|
.toList();
|
|
}
|
|
}
|
|
|
|
// Adds a product to the cart.
|
|
void addProductToCart(int productId) {
|
|
if (!_productsInCart.containsKey(productId)) {
|
|
_productsInCart[productId] = 1;
|
|
} else {
|
|
_productsInCart[productId]++;
|
|
}
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
// Removes an item from the cart.
|
|
void removeItemFromCart(int productId) {
|
|
if (_productsInCart.containsKey(productId)) {
|
|
if (_productsInCart[productId] == 1) {
|
|
_productsInCart.remove(productId);
|
|
} else {
|
|
_productsInCart[productId]--;
|
|
}
|
|
}
|
|
|
|
notifyListeners();
|
|
}
|
|
|
|
// Returns the Product instance matching the provided id.
|
|
Product getProductById(int id) {
|
|
return _availableProducts.firstWhere((p) => p.id == id);
|
|
}
|
|
|
|
// Removes everything from the cart.
|
|
void clearCart() {
|
|
_productsInCart.clear();
|
|
notifyListeners();
|
|
}
|
|
|
|
// Loads the list of available products from the repo.
|
|
void loadProducts() {
|
|
_availableProducts = ProductsRepository.loadProducts(categoryAll);
|
|
notifyListeners();
|
|
}
|
|
|
|
void setCategory(Category newCategory) {
|
|
_selectedCategory = newCategory;
|
|
notifyListeners();
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
return 'AppStateModel(totalCost: $totalCost)';
|
|
}
|
|
}
|