From c061e361ebd08f15541f4e40a2b3279883229bee Mon Sep 17 00:00:00 2001 From: Jonas Franz Date: Sat, 19 Feb 2022 14:41:37 +0100 Subject: [PATCH] Add product selection store --- lib/models/product.dart | 4 +++- lib/stores/product_selection_store.dart | 31 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 lib/stores/product_selection_store.dart diff --git a/lib/models/product.dart b/lib/models/product.dart index 1b73556..32b4f05 100644 --- a/lib/models/product.dart +++ b/lib/models/product.dart @@ -1,8 +1,10 @@ import 'package:flutter/foundation.dart'; +typedef ProductTitle = String; + @immutable class Product { - final String title; + final ProductTitle title; final double price; String get priceAsString => price.toStringAsFixed(2); diff --git a/lib/stores/product_selection_store.dart b/lib/stores/product_selection_store.dart new file mode 100644 index 0000000..168a4c4 --- /dev/null +++ b/lib/stores/product_selection_store.dart @@ -0,0 +1,31 @@ +import 'package:flutter/material.dart'; +import 'package:thesis_shop/models/product.dart'; + +class ProductSelectionStore extends InheritedWidget { + const ProductSelectionStore({ + Key? key, + required this.products, + required this.productAmounts, + required this.increaseAmount, + required this.decreaseAmount, + required Widget child, + }) : super(key: key, child: child); + + final List products; + final Map productAmounts; + + final Function(Product) increaseAmount; + final Function(Product) decreaseAmount; + + static ProductSelectionStore of(BuildContext context) { + final ProductSelectionStore? result = + context.dependOnInheritedWidgetOfExactType(); + assert(result != null, 'No ProductSelectionStore found in context'); + return result!; + } + + @override + bool updateShouldNotify(ProductSelectionStore oldWidget) { + return productAmounts != oldWidget.productAmounts; + } +}