From a76d3a0f3568296c530a8310eb8b245a05101d45 Mon Sep 17 00:00:00 2001 From: Patrick Alvin Alcala Date: Tue, 11 Mar 2025 16:22:46 +0800 Subject: [PATCH] update --- .../customer_pages/customer_search_page.dart | 245 ++++++++++-------- lib/tables/cart.dart | 17 ++ lib/widgets/item_card_widget.dart | 31 ++- 3 files changed, 175 insertions(+), 118 deletions(-) create mode 100644 lib/tables/cart.dart diff --git a/lib/pages/customer_pages/customer_search_page.dart b/lib/pages/customer_pages/customer_search_page.dart index 4a7c400..975abca 100644 --- a/lib/pages/customer_pages/customer_search_page.dart +++ b/lib/pages/customer_pages/customer_search_page.dart @@ -1,6 +1,9 @@ +import 'dart:developer'; + import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; -import 'package:pharmacy_mobile/tables/ref_medicines.dart'; +import 'package:pharmacy_mobile/auth/auth_service.dart'; +import 'package:pharmacy_mobile/tables/cart.dart'; import 'package:pharmacy_mobile/tables/stocks.dart'; import 'package:pharmacy_mobile/tables/storage.dart'; import 'package:pharmacy_mobile/widgets/customer_pagebackground_widget.dart'; @@ -18,12 +21,15 @@ class CustomerSearchPage extends StatefulWidget { class _CustomerSearchPageState extends State { final _searchController = TextEditingController(); + final _authService = AuthService(); final _storage = Storage(); // final _refMedicines = RefMedicines(); final _stocks = Stocks(); + final _carts = Carts(); // final _refMedicines = RefMedicines(); late String imageUrl = ''; late List _stockList = []; + late List _cartList = []; // late final List _medicinesList = []; Future _getURL() async { @@ -37,6 +43,18 @@ class _CustomerSearchPageState extends State { _stockList = await _stocks.getList(); } + Future _getCurrentUser() async { + final result = _authService.getCurrentUser(); + return result.toString(); + } + + Future _getCarts() async { + final currentUser = await _getCurrentUser(); + log('currentUser: $currentUser'); + _cartList = await _carts.getCart(currentUser); + log('cart: ${_cartList.toString()}'); + } + // Future _getAllMedicines() async { // _medicinesList = await _refMedicines.getList(); // } @@ -63,6 +81,7 @@ class _CustomerSearchPageState extends State { void autoRun() async { await _getAllStocks(); + await _getCarts(); // await _getAllMedicines(); await _getURL(); } @@ -141,41 +160,41 @@ class _CustomerSearchPageState extends State { const Row( children: [ ItemCardWidget( - imageUrl: 'imageUrl', - text: 'item[medicine_name]', - subtext: 'item[generic_name]', - price: 123, - quantity: 123, - isLoading: true, - ), + imageUrl: 'imageUrl', + text: 'item[medicine_name]', + subtext: 'item[generic_name]', + price: 123, + quantity: 123, + isLoading: true, + cart: 0), ItemCardWidget( - imageUrl: 'imageUrl', - text: 'item[medicine_name]', - subtext: 'item[generic_name]', - price: 123, - quantity: 123, - isLoading: true, - ), + imageUrl: 'imageUrl', + text: 'item[medicine_name]', + subtext: 'item[generic_name]', + price: 123, + quantity: 123, + isLoading: true, + cart: 0), ItemCardWidget( - imageUrl: 'imageUrl', - text: 'item[medicine_name]', - subtext: 'item[generic_name]', - price: 123, - quantity: 123, - isLoading: true, - ) + imageUrl: 'imageUrl', + text: 'item[medicine_name]', + subtext: 'item[generic_name]', + price: 123, + quantity: 123, + isLoading: true, + cart: 0) ], ) else for (var item in _stockList) ItemCardWidget( - imageUrl: imageUrl, - text: item['medicine_name'].toString(), - subtext: item['generic_name'].toString(), - price: item['price'].toDouble(), - quantity: item['quantity'].toDouble(), - isLoading: false, - ), + imageUrl: imageUrl, + text: item['medicine_name'].toString(), + subtext: item['generic_name'].toString(), + price: item['price'].toDouble(), + quantity: item['quantity'].toDouble(), + isLoading: false, + cart: 0), ], )), const Gap(16), @@ -186,29 +205,29 @@ class _CustomerSearchPageState extends State { child: Row( children: [ ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 0, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 0, + isLoading: imageUrl.isEmpty, + cart: 2), ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ], )), const Gap(16), @@ -219,29 +238,29 @@ class _CustomerSearchPageState extends State { child: Row( children: [ ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ], )), const Gap(16), @@ -252,29 +271,29 @@ class _CustomerSearchPageState extends State { child: Row( children: [ ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ], )), const Gap(16), @@ -285,29 +304,29 @@ class _CustomerSearchPageState extends State { child: Row( children: [ ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ItemCardWidget( - imageUrl: imageUrl, - text: 'sample', - subtext: 'generic_name', - price: 500, - quantity: 15, - isLoading: imageUrl.isEmpty, - ), + imageUrl: imageUrl, + text: 'sample', + subtext: 'generic_name', + price: 500, + quantity: 15, + isLoading: imageUrl.isEmpty, + cart: 0), ], )), ], diff --git a/lib/tables/cart.dart b/lib/tables/cart.dart new file mode 100644 index 0000000..d4f1f93 --- /dev/null +++ b/lib/tables/cart.dart @@ -0,0 +1,17 @@ +import 'dart:developer'; + +import 'package:supabase_flutter/supabase_flutter.dart'; + +class Carts { + final SupabaseClient _supabase = Supabase.instance.client; + + Future getCart(String email) async { + try { + final data = await _supabase.from('carts').select('carts_uuid').eq('email', email); + log('data: $data'); + return data.toList(); + } catch (e) { + return []; + } + } +} diff --git a/lib/widgets/item_card_widget.dart b/lib/widgets/item_card_widget.dart index 7620a47..65c6b71 100644 --- a/lib/widgets/item_card_widget.dart +++ b/lib/widgets/item_card_widget.dart @@ -12,6 +12,7 @@ class ItemCardWidget extends StatelessWidget { final double price; final double quantity; final bool isLoading; + final double cart; const ItemCardWidget( {super.key, @@ -20,7 +21,8 @@ class ItemCardWidget extends StatelessWidget { required this.subtext, required this.price, required this.quantity, - required this.isLoading}); + required this.isLoading, + required this.cart}); @override Widget build(BuildContext context) { @@ -110,13 +112,32 @@ class ItemCardWidget extends StatelessWidget { Container( padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 16), decoration: BoxDecoration( + color: cart > 0 ? const Color.fromRGBO(99, 44, 113, 1) : const Color.fromRGBO(255, 255, 255, 1), border: Border.all(color: Colors.black), borderRadius: BorderRadius.circular(20), ), - child: FaIcon( - FontAwesomeIcons.cartShopping, - size: 12, - )) + child: (cart > 0) + ? Row( + children: [ + const FaIcon( + FontAwesomeIcons.cartShopping, + size: 12, + color: Color.fromRGBO(255, 255, 255, 1), + ), + const Gap(4), + TextWidget( + text: cart.toStringAsFixed(0), + size: 12, + color: const Color.fromRGBO(255, 255, 255, 1), + bold: true, + ) + ], + ) + : const FaIcon( + FontAwesomeIcons.cartShopping, + size: 12, + color: Color.fromRGBO(0, 0, 0, 1), + )) ], ), ),