From 3113e771e08699051a06364e6504d5934f469a2a Mon Sep 17 00:00:00 2001 From: Mikkel Troels Kongsted Date: Tue, 4 Feb 2025 14:31:09 +0100 Subject: [PATCH] feedback when adding to cart --- mobile/lib/main.dart | 2 ++ mobile/lib/pages/product_page.dart | 16 ++++++++++++++-- mobile/lib/repos/add_to_cart_state.dart | 15 +++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 mobile/lib/repos/add_to_cart_state.dart diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart index fcb0f49..d6498c2 100644 --- a/mobile/lib/main.dart +++ b/mobile/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:mobile/repos/add_to_cart_state.dart'; import 'package:mobile/repos/cart.dart'; import 'package:mobile/repos/paying_state.dart'; import 'package:mobile/repos/product.dart'; @@ -23,6 +24,7 @@ class MyApp extends StatelessWidget { ChangeNotifierProvider(create: (_) => CartRepo()), ChangeNotifierProvider(create: (_) => ReceiptRepo()), ChangeNotifierProvider(create: (_) => PayingStateRepo()), + ChangeNotifierProvider(create: (_) => AddToCartStateRepo()), ], child: MaterialApp( title: 'Fresh Plaza', diff --git a/mobile/lib/pages/product_page.dart b/mobile/lib/pages/product_page.dart index fb79c6f..60c4c73 100644 --- a/mobile/lib/pages/product_page.dart +++ b/mobile/lib/pages/product_page.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:mobile/repos/add_to_cart_state.dart'; import 'package:mobile/repos/cart.dart'; import 'package:mobile/repos/product.dart'; import 'package:mobile/widgets/primary_button.dart'; @@ -11,6 +12,8 @@ class ProductPage extends StatelessWidget { @override Widget build(BuildContext context) { + final AddToCartStateRepo addToCartStateRepo = + context.watch(); return Scaffold( body: Card( margin: const EdgeInsets.all(10), @@ -70,14 +73,23 @@ class ProductPage extends StatelessWidget { onPressed: () {}, child: const Text("Find i butik")), PrimaryButton( onPressed: () { - var cartRepo = context.read(); + final snackBarDuration = const Duration(seconds: 2); + addToCartStateRepo.notify(snackBarDuration); + final snackBar = SnackBar( + content: Text( + 'Tilføjet ${addToCartStateRepo.currentAmount} ${product.name} til kurven'), + duration: const Duration(seconds: 2), + ); + ScaffoldMessenger.of(context).removeCurrentSnackBar(); + final cartRepo = context.read(); cartRepo.addToCart(product); + ScaffoldMessenger.of(context).showSnackBar(snackBar); }, child: const Text("Tilføj til indkøbskurv")), ], ), ), - ) + ), ]), ), ), diff --git a/mobile/lib/repos/add_to_cart_state.dart b/mobile/lib/repos/add_to_cart_state.dart new file mode 100644 index 0000000..ecb4b33 --- /dev/null +++ b/mobile/lib/repos/add_to_cart_state.dart @@ -0,0 +1,15 @@ +import 'dart:async'; + +import 'package:flutter/material.dart'; + +class AddToCartStateRepo extends ChangeNotifier { + int currentAmount = 0; + Timer resetTimer = Timer(const Duration(), () {}); + + void notify(Duration displayDuration) { + resetTimer.cancel(); + currentAmount++; + notifyListeners(); + resetTimer = Timer(displayDuration, () => currentAmount = 0); + } +}