211 lines
7.4 KiB
Dart
211 lines
7.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../../app/app_services.dart';
|
|
import '../../../app/router/app_router.dart';
|
|
import '../../../core/utils/currency_formatter.dart';
|
|
import '../../../core/widgets/app_button.dart';
|
|
import '../../../core/widgets/empty_view.dart';
|
|
import '../../../domain/entities/cart_item.dart';
|
|
|
|
class CartScreen extends StatelessWidget {
|
|
const CartScreen({required this.services, super.key});
|
|
|
|
final AppServices services;
|
|
|
|
Future<void> _confirmClear(BuildContext context) async {
|
|
final shouldClear = await showDialog<bool>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Limpar carrinho?'),
|
|
content: const Text('Todos os produtos serão removidos do carrinho.'),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(false),
|
|
child: const Text('Cancelar'),
|
|
),
|
|
FilledButton(
|
|
onPressed: () => Navigator.of(context).pop(true),
|
|
child: const Text('Limpar'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
if (shouldClear == true) services.cartRepository.clear();
|
|
}
|
|
|
|
Widget _buildCartItem(BuildContext context, CartItem item) {
|
|
final repository = services.cartRepository;
|
|
return Card(
|
|
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 5),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(12),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Text(
|
|
item.product.name,
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
maxLines: 2,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
const SizedBox(height: 4),
|
|
Text(
|
|
'Código ${item.product.code} · Grade ${item.product.grade}',
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
),
|
|
const SizedBox(height: 8),
|
|
Row(
|
|
children: [
|
|
Expanded(
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
Text(
|
|
'Unitário: ${CurrencyFormatter.formatBrl(item.product.priceMinorUnits)}',
|
|
),
|
|
Text(
|
|
'Subtotal: ${CurrencyFormatter.formatBrl(item.subtotalMinorUnits)}',
|
|
style: const TextStyle(fontWeight: FontWeight.w600),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
IconButton(
|
|
key: Key('decrease-${item.product.cartKey}'),
|
|
tooltip: 'Diminuir quantidade',
|
|
onPressed: item.quantity <= 1
|
|
? null
|
|
: () => repository.decreaseQuantity(item.product),
|
|
icon: const Icon(Icons.remove_circle_outline),
|
|
),
|
|
Text(
|
|
'${item.quantity}',
|
|
key: Key('quantity-${item.product.cartKey}'),
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
),
|
|
IconButton(
|
|
key: Key('increase-${item.product.cartKey}'),
|
|
tooltip: 'Aumentar quantidade',
|
|
onPressed: () => repository.increaseQuantity(item.product),
|
|
icon: const Icon(Icons.add_circle_outline),
|
|
),
|
|
IconButton(
|
|
key: Key('remove-${item.product.cartKey}'),
|
|
tooltip: 'Remover produto',
|
|
onPressed: () => repository.removeProduct(item.product),
|
|
icon: Icon(
|
|
Icons.delete_outline,
|
|
color: Theme.of(context).colorScheme.error,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Carrinho'),
|
|
actions: [
|
|
AnimatedBuilder(
|
|
animation: services.cartRepository,
|
|
builder: (context, _) => IconButton(
|
|
key: const Key('clear-cart-button'),
|
|
tooltip: 'Limpar carrinho',
|
|
onPressed: services.cartRepository.itemCount == 0
|
|
? null
|
|
: () => _confirmClear(context),
|
|
icon: const Icon(Icons.delete_sweep_outlined),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
body: AnimatedBuilder(
|
|
animation: services.cartRepository,
|
|
builder: (context, _) {
|
|
final items = services.cartRepository.items;
|
|
if (items.isEmpty) {
|
|
return Column(
|
|
children: [
|
|
const Expanded(
|
|
child: EmptyView(
|
|
title: 'Carrinho vazio',
|
|
message: 'Adicione produtos na tela de venda.',
|
|
icon: Icons.shopping_cart_outlined,
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: AppButton(
|
|
label: 'Voltar para venda',
|
|
icon: Icons.storefront_outlined,
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
final total = services.cartRepository.totalMinorUnits;
|
|
return Column(
|
|
children: [
|
|
Expanded(
|
|
child: ListView.builder(
|
|
padding: const EdgeInsets.only(top: 8, bottom: 8),
|
|
itemCount: items.length,
|
|
itemBuilder: (context, index) =>
|
|
_buildCartItem(context, items[index]),
|
|
),
|
|
),
|
|
Material(
|
|
elevation: 4,
|
|
color: Theme.of(context).colorScheme.surface,
|
|
child: SafeArea(
|
|
top: false,
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(16),
|
|
child: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
const Expanded(
|
|
child: Text(
|
|
'Total (prévia)',
|
|
style: TextStyle(fontWeight: FontWeight.w600),
|
|
),
|
|
),
|
|
Text(
|
|
total == null
|
|
? 'Total indisponível'
|
|
: CurrencyFormatter.formatBrl(total),
|
|
key: const Key('cart-total'),
|
|
style: Theme.of(context).textTheme.titleLarge,
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 10),
|
|
AppButton(
|
|
label: 'Pagamento',
|
|
icon: Icons.payments_outlined,
|
|
onPressed: () => Navigator.of(context)
|
|
.pushNamed(AppRoutes.payment),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|