This commit is contained in:
Phuoc Nguyen
2025-10-10 16:38:07 +07:00
parent e5b247d622
commit b94c158004
177 changed files with 25080 additions and 152 deletions

View File

@@ -0,0 +1,173 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../widgets/product_selector.dart';
import '../widgets/cart_summary.dart';
import '../providers/cart_provider.dart';
import '../../domain/entities/cart_item.dart';
/// Home page - POS interface with product selector and cart
class HomePage extends ConsumerWidget {
const HomePage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final cartAsync = ref.watch(cartProvider);
final isWideScreen = MediaQuery.of(context).size.width > 600;
return Scaffold(
appBar: AppBar(
title: const Text('Point of Sale'),
actions: [
// Cart item count badge
cartAsync.whenOrNull(
data: (items) => items.isNotEmpty
? Padding(
padding: const EdgeInsets.only(right: 16.0),
child: Center(
child: Badge(
label: Text('${items.length}'),
child: const Icon(Icons.shopping_cart),
),
),
)
: null,
) ?? const SizedBox.shrink(),
],
),
body: isWideScreen
? Row(
children: [
// Product selector on left
Expanded(
flex: 3,
child: ProductSelector(
onProductTap: (product) {
_showAddToCartDialog(context, ref, product);
},
),
),
// Divider
const VerticalDivider(width: 1),
// Cart on right
const Expanded(
flex: 2,
child: CartSummary(),
),
],
)
: Column(
children: [
// Product selector on top
Expanded(
flex: 2,
child: ProductSelector(
onProductTap: (product) {
_showAddToCartDialog(context, ref, product);
},
),
),
// Divider
const Divider(height: 1),
// Cart on bottom
const Expanded(
flex: 3,
child: CartSummary(),
),
],
),
);
}
void _showAddToCartDialog(
BuildContext context,
WidgetRef ref,
dynamic product,
) {
int quantity = 1;
showDialog(
context: context,
builder: (context) => StatefulBuilder(
builder: (context, setState) => AlertDialog(
title: const Text('Add to Cart'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
product.name,
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: const Icon(Icons.remove_circle_outline),
onPressed: quantity > 1
? () => setState(() => quantity--)
: null,
),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Text(
'$quantity',
style: Theme.of(context).textTheme.headlineSmall,
),
),
IconButton(
icon: const Icon(Icons.add_circle_outline),
onPressed: quantity < product.stockQuantity
? () => setState(() => quantity++)
: null,
),
],
),
if (product.stockQuantity < 5)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
'Only ${product.stockQuantity} in stock',
style: Theme.of(context).textTheme.bodySmall?.copyWith(
color: Theme.of(context).colorScheme.error,
),
),
),
],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('Cancel'),
),
FilledButton.icon(
onPressed: () {
// Create cart item from product
final cartItem = CartItem(
productId: product.id,
productName: product.name,
price: product.price,
quantity: quantity,
imageUrl: product.imageUrl,
addedAt: DateTime.now(),
);
// Add to cart
ref.read(cartProvider.notifier).addItem(cartItem);
Navigator.pop(context);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Added ${product.name} to cart'),
duration: const Duration(seconds: 2),
),
);
},
icon: const Icon(Icons.add_shopping_cart),
label: const Text('Add'),
),
],
),
),
);
}
}

View File

@@ -0,0 +1,27 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'cart_provider.dart';
part 'cart_item_count_provider.g.dart';
/// Provider that calculates total number of items in cart
/// This is optimized to only rebuild when the count changes
@riverpod
int cartItemCount(Ref ref) {
final itemsAsync = ref.watch(cartProvider);
return itemsAsync.when(
data: (items) => items.fold<int>(0, (sum, item) => sum + item.quantity),
loading: () => 0,
error: (_, __) => 0,
);
}
/// Provider that calculates unique items count in cart
@riverpod
int cartUniqueItemCount(Ref ref) {
final itemsAsync = ref.watch(cartProvider);
return itemsAsync.when(
data: (items) => items.length,
loading: () => 0,
error: (_, __) => 0,
);
}

View File

@@ -0,0 +1,104 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'cart_item_count_provider.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
/// Provider that calculates total number of items in cart
/// This is optimized to only rebuild when the count changes
@ProviderFor(cartItemCount)
const cartItemCountProvider = CartItemCountProvider._();
/// Provider that calculates total number of items in cart
/// This is optimized to only rebuild when the count changes
final class CartItemCountProvider extends $FunctionalProvider<int, int, int>
with $Provider<int> {
/// Provider that calculates total number of items in cart
/// This is optimized to only rebuild when the count changes
const CartItemCountProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'cartItemCountProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$cartItemCountHash();
@$internal
@override
$ProviderElement<int> $createElement($ProviderPointer pointer) =>
$ProviderElement(pointer);
@override
int create(Ref ref) {
return cartItemCount(ref);
}
/// {@macro riverpod.override_with_value}
Override overrideWithValue(int value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<int>(value),
);
}
}
String _$cartItemCountHash() => r'78fe81648a02fb84477df3be3f08b27caa039203';
/// Provider that calculates unique items count in cart
@ProviderFor(cartUniqueItemCount)
const cartUniqueItemCountProvider = CartUniqueItemCountProvider._();
/// Provider that calculates unique items count in cart
final class CartUniqueItemCountProvider
extends $FunctionalProvider<int, int, int>
with $Provider<int> {
/// Provider that calculates unique items count in cart
const CartUniqueItemCountProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'cartUniqueItemCountProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$cartUniqueItemCountHash();
@$internal
@override
$ProviderElement<int> $createElement($ProviderPointer pointer) =>
$ProviderElement(pointer);
@override
int create(Ref ref) {
return cartUniqueItemCount(ref);
}
/// {@macro riverpod.override_with_value}
Override overrideWithValue(int value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<int>(value),
);
}
}
String _$cartUniqueItemCountHash() =>
r'51eec092c957d0d4819200fd935115db77c7f8d3';

View File

@@ -0,0 +1,54 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../../domain/entities/cart_item.dart';
part 'cart_provider.g.dart';
/// Provider for shopping cart
@riverpod
class Cart extends _$Cart {
@override
Future<List<CartItem>> build() async {
// TODO: Implement with repository
return [];
}
Future<void> addItem(CartItem item) async {
// TODO: Implement add to cart
state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
final currentItems = state.value ?? [];
return [...currentItems, item];
});
}
Future<void> removeItem(String productId) async {
// TODO: Implement remove from cart
state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
final currentItems = state.value ?? [];
return currentItems.where((item) => item.productId != productId).toList();
});
}
Future<void> updateQuantity(String productId, int quantity) async {
// TODO: Implement update quantity
state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
final currentItems = state.value ?? [];
return currentItems.map((item) {
if (item.productId == productId) {
return item.copyWith(quantity: quantity);
}
return item;
}).toList();
});
}
Future<void> clearCart() async {
// TODO: Implement clear cart
state = const AsyncValue.loading();
state = await AsyncValue.guard(() async {
return [];
});
}
}

View File

@@ -0,0 +1,59 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'cart_provider.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
/// Provider for shopping cart
@ProviderFor(Cart)
const cartProvider = CartProvider._();
/// Provider for shopping cart
final class CartProvider extends $AsyncNotifierProvider<Cart, List<CartItem>> {
/// Provider for shopping cart
const CartProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'cartProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$cartHash();
@$internal
@override
Cart create() => Cart();
}
String _$cartHash() => r'0136ac2c2a04412a130184e30c01e33a17b0d4db';
/// Provider for shopping cart
abstract class _$Cart extends $AsyncNotifier<List<CartItem>> {
FutureOr<List<CartItem>> build();
@$mustCallSuper
@override
void runBuild() {
final created = build();
final ref = this.ref as $Ref<AsyncValue<List<CartItem>>, List<CartItem>>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<AsyncValue<List<CartItem>>, List<CartItem>>,
AsyncValue<List<CartItem>>,
Object?,
Object?
>;
element.handleValue(ref, created);
}
}

View File

@@ -0,0 +1,83 @@
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'cart_provider.dart';
import '../../../settings/presentation/providers/settings_provider.dart';
part 'cart_total_provider.g.dart';
/// Cart totals calculation provider
@riverpod
class CartTotal extends _$CartTotal {
@override
CartTotalData build() {
final itemsAsync = ref.watch(cartProvider);
final settingsAsync = ref.watch(settingsProvider);
final items = itemsAsync.when(
data: (data) => data,
loading: () => <dynamic>[],
error: (_, __) => <dynamic>[],
);
final settings = settingsAsync.when(
data: (data) => data,
loading: () => null,
error: (_, __) => null,
);
// Calculate subtotal
final subtotal = items.fold<double>(
0.0,
(sum, item) => sum + item.lineTotal,
);
// Calculate tax
final taxRate = settings?.taxRate ?? 0.0;
final tax = subtotal * taxRate;
// Calculate total
final total = subtotal + tax;
return CartTotalData(
subtotal: subtotal,
tax: tax,
taxRate: taxRate,
total: total,
itemCount: items.length,
);
}
/// Apply discount amount to total
double applyDiscount(double discountAmount) {
final currentTotal = state.total;
return (currentTotal - discountAmount).clamp(0.0, double.infinity);
}
/// Apply discount percentage to total
double applyDiscountPercentage(double discountPercent) {
final currentTotal = state.total;
final discountAmount = currentTotal * (discountPercent / 100);
return (currentTotal - discountAmount).clamp(0.0, double.infinity);
}
}
/// Cart total data model
class CartTotalData {
final double subtotal;
final double tax;
final double taxRate;
final double total;
final int itemCount;
const CartTotalData({
required this.subtotal,
required this.tax,
required this.taxRate,
required this.total,
required this.itemCount,
});
@override
String toString() {
return 'CartTotalData(subtotal: $subtotal, tax: $tax, total: $total, items: $itemCount)';
}
}

View File

@@ -0,0 +1,68 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'cart_total_provider.dart';
// **************************************************************************
// RiverpodGenerator
// **************************************************************************
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint, type=warning
/// Cart totals calculation provider
@ProviderFor(CartTotal)
const cartTotalProvider = CartTotalProvider._();
/// Cart totals calculation provider
final class CartTotalProvider
extends $NotifierProvider<CartTotal, CartTotalData> {
/// Cart totals calculation provider
const CartTotalProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'cartTotalProvider',
isAutoDispose: true,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$cartTotalHash();
@$internal
@override
CartTotal create() => CartTotal();
/// {@macro riverpod.override_with_value}
Override overrideWithValue(CartTotalData value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<CartTotalData>(value),
);
}
}
String _$cartTotalHash() => r'044f6d4749eec49f9ef4173fc42d149a3841df21';
/// Cart totals calculation provider
abstract class _$CartTotal extends $Notifier<CartTotalData> {
CartTotalData build();
@$mustCallSuper
@override
void runBuild() {
final created = build();
final ref = this.ref as $Ref<CartTotalData, CartTotalData>;
final element =
ref.element
as $ClassProviderElement<
AnyNotifier<CartTotalData, CartTotalData>,
CartTotalData,
Object?,
Object?
>;
element.handleValue(ref, created);
}
}

View File

@@ -0,0 +1,4 @@
/// Export all home/cart providers
export 'cart_provider.dart';
export 'cart_total_provider.dart';
export 'cart_item_count_provider.dart';

View File

@@ -0,0 +1,67 @@
import 'package:flutter/material.dart';
import '../../domain/entities/cart_item.dart';
import '../../../../shared/widgets/price_display.dart';
/// Cart item card widget
class CartItemCard extends StatelessWidget {
final CartItem item;
final VoidCallback? onRemove;
final Function(int)? onQuantityChanged;
const CartItemCard({
super.key,
required this.item,
this.onRemove,
this.onQuantityChanged,
});
@override
Widget build(BuildContext context) {
return Card(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
item.productName,
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 4),
PriceDisplay(price: item.price),
],
),
),
Row(
children: [
IconButton(
icon: const Icon(Icons.remove_circle_outline),
onPressed: item.quantity > 1
? () => onQuantityChanged?.call(item.quantity - 1)
: null,
),
Text(
'${item.quantity}',
style: Theme.of(context).textTheme.titleMedium,
),
IconButton(
icon: const Icon(Icons.add_circle_outline),
onPressed: () => onQuantityChanged?.call(item.quantity + 1),
),
],
),
IconButton(
icon: const Icon(Icons.delete_outline),
onPressed: onRemove,
color: Theme.of(context).colorScheme.error,
),
],
),
),
);
}
}

View File

@@ -0,0 +1,128 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../providers/cart_provider.dart';
import '../providers/cart_total_provider.dart';
import 'cart_item_card.dart';
import '../../../../shared/widgets/price_display.dart';
import '../../../../core/widgets/empty_state.dart';
/// Cart summary widget
class CartSummary extends ConsumerWidget {
const CartSummary({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final cartAsync = ref.watch(cartProvider);
final totalData = ref.watch(cartTotalProvider);
return Container(
decoration: BoxDecoration(
border: Border(
top: BorderSide(
color: Theme.of(context).dividerColor,
width: 1,
),
),
),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Shopping Cart',
style: Theme.of(context).textTheme.titleLarge,
),
if (cartAsync.value?.isNotEmpty ?? false)
TextButton.icon(
onPressed: () {
ref.read(cartProvider.notifier).clearCart();
},
icon: const Icon(Icons.delete_sweep),
label: const Text('Clear'),
),
],
),
),
Expanded(
child: cartAsync.when(
loading: () => const Center(child: CircularProgressIndicator()),
error: (error, stack) => Center(child: Text('Error: $error')),
data: (items) {
if (items.isEmpty) {
return const EmptyState(
message: 'Cart is empty',
subMessage: 'Add products to get started',
icon: Icons.shopping_cart_outlined,
);
}
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) {
final item = items[index];
return CartItemCard(
item: item,
onRemove: () {
ref.read(cartProvider.notifier).removeItem(item.productId);
},
onQuantityChanged: (quantity) {
ref.read(cartProvider.notifier).updateQuantity(
item.productId,
quantity,
);
},
);
},
);
},
),
),
Container(
padding: const EdgeInsets.all(16.0),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHighest,
),
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
'Total:',
style: Theme.of(context).textTheme.titleLarge,
),
PriceDisplay(
price: totalData.total,
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.bold,
),
),
],
),
const SizedBox(height: 16),
SizedBox(
width: double.infinity,
child: ElevatedButton.icon(
onPressed: (cartAsync.value?.isNotEmpty ?? false)
? () {
// TODO: Implement checkout
}
: null,
icon: const Icon(Icons.payment),
label: const Text('Checkout'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.all(16),
),
),
),
],
),
),
],
),
);
}
}

View File

@@ -0,0 +1,98 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../products/presentation/providers/products_provider.dart';
import '../../../products/presentation/widgets/product_card.dart';
import '../../../products/domain/entities/product.dart';
import '../../../../core/widgets/loading_indicator.dart';
import '../../../../core/widgets/error_widget.dart';
import '../../../../core/widgets/empty_state.dart';
/// Product selector widget for POS
class ProductSelector extends ConsumerWidget {
final void Function(Product)? onProductTap;
const ProductSelector({
super.key,
this.onProductTap,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final productsAsync = ref.watch(productsProvider);
return Container(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Select Products',
style: Theme.of(context).textTheme.titleLarge,
),
const SizedBox(height: 16),
Expanded(
child: productsAsync.when(
loading: () => const LoadingIndicator(
message: 'Loading products...',
),
error: (error, stack) => ErrorDisplay(
message: error.toString(),
onRetry: () => ref.refresh(productsProvider),
),
data: (products) {
if (products.isEmpty) {
return const EmptyState(
message: 'No products available',
subMessage: 'Add products to start selling',
icon: Icons.inventory_2_outlined,
);
}
// Filter only available products for POS
final availableProducts =
products.where((p) => p.isAvailable).toList();
if (availableProducts.isEmpty) {
return const EmptyState(
message: 'No products available',
subMessage: 'All products are currently unavailable',
icon: Icons.inventory_2_outlined,
);
}
return LayoutBuilder(
builder: (context, constraints) {
// Determine grid columns based on width
int crossAxisCount = 2;
if (constraints.maxWidth > 800) {
crossAxisCount = 4;
} else if (constraints.maxWidth > 600) {
crossAxisCount = 3;
}
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: crossAxisCount,
childAspectRatio: 0.75,
crossAxisSpacing: 12,
mainAxisSpacing: 12,
),
itemCount: availableProducts.length,
itemBuilder: (context, index) {
final product = availableProducts[index];
return GestureDetector(
onTap: () => onProductTap?.call(product),
child: ProductCard(product: product),
);
},
);
},
);
},
),
),
],
),
);
}
}

View File

@@ -0,0 +1,5 @@
// Home/Cart Feature Widgets
export 'cart_item_card.dart';
export 'cart_summary.dart';
// This file provides a central export point for all home/cart widgets