Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // scoped-models/products.dart
- import 'package:scoped_model/scoped_model.dart';
- import '../models/product.dart';
- class ProductsModel extends Model {
- List<Product> _products = [];
- int? _selectedProductIndex; // if we changed from var => int ?
- bool _showFavorites = false;
- List<Product> get products {
- return List.from(_products);
- }
- List<Product> get displayedProducts {
- if (_showFavorites) {
- return _products.where((Product product) => product.isFavourite).toList();
- }
- return List.from(_products);
- }
- int get selectedProductIndex{
- return _selectedProductIndex!.toInt();
- }
- Product get selectedProduct {
- // Error gone here :)
- // But need to dive ..
- //return _selectedProductIndex;
- //If i uncomment underline :: Error happened
- return _products[_selectedProductIndex!.toInt()];
- }
- bool get displayFavoritesOnly {
- return _showFavorites;
- }
- void addProduct(Product product) {
- _products.add(product);
- //Chnaged to null frrom 0
- // _selectedProductIndex = null;
- notifyListeners();
- }
- void updateProduct(Product product) {
- _products[_selectedProductIndex!.toInt()] = product;
- //_selectedProductIndex = null;
- notifyListeners();
- }
- void deleteProduct() {
- _products.removeAt(_selectedProductIndex!.toInt());
- //_selectedProductIndex = null;
- notifyListeners();
- }
- void toggleProductFavoriteStatus() {
- final bool isCurrenltyFavorite = selectedProduct.isFavourite;
- final bool newFavoriteStatus = !isCurrenltyFavorite;
- final Product updateProduct = Product(
- title: selectedProduct.title,
- description: selectedProduct.description,
- price: selectedProduct.price,
- image: selectedProduct.image,
- isFavourite: newFavoriteStatus);
- _products[selectedProductIndex] = updateProduct;
- notifyListeners();
- }
- void selectProduct(int index) {
- _selectedProductIndex = index;
- notifyListeners();
- }
- void toggleDisplayMode() {
- _showFavorites = !_showFavorites;
- notifyListeners();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement