181186 - Criar PDV Android para rodar em maquinas P.O.S.
This commit is contained in:
commit
c9983cbad5
204 files changed
+11453
No files matched your search
@@ -0,0 +1,255 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../app/app_services.dart';
|
||||
import '../../../core/utils/currency_formatter.dart';
|
||||
import '../../../domain/entities/historical_sale.dart';
|
||||
import '../../../domain/repositories/sales_history_repository.dart';
|
||||
|
||||
class SalesHistoryScreen extends StatefulWidget {
|
||||
const SalesHistoryScreen({required this.services, super.key});
|
||||
|
||||
final AppServices services;
|
||||
|
||||
@override
|
||||
State<SalesHistoryScreen> createState() => _SalesHistoryScreenState();
|
||||
}
|
||||
|
||||
class _SalesHistoryScreenState extends State<SalesHistoryScreen> {
|
||||
late Future<List<HistoricalSale>> _salesFuture;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_salesFuture = widget.services.salesHistoryRepository.fetchSales();
|
||||
}
|
||||
|
||||
Future<void> _reload() async {
|
||||
final future = widget.services.salesHistoryRepository.fetchSales();
|
||||
setState(() => _salesFuture = future);
|
||||
try {
|
||||
await future;
|
||||
} catch (_) {
|
||||
// FutureBuilder displays the error while RefreshIndicator is dismissed.
|
||||
}
|
||||
}
|
||||
|
||||
String _formatDate(String value) {
|
||||
final parts = value.split('-');
|
||||
if (parts.length != 3) return value;
|
||||
return '${parts[2]}/${parts[1]}/${parts[0]}';
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Histórico de vendas')),
|
||||
body: SafeArea(
|
||||
child: FutureBuilder<List<HistoricalSale>>(
|
||||
future: _salesFuture,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState == ConnectionState.waiting) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
final error = snapshot.error;
|
||||
final message = error is SalesHistoryException
|
||||
? error.message
|
||||
: 'Não foi possível carregar o histórico de vendas.';
|
||||
return Center(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Icon(Icons.error_outline, size: 48),
|
||||
const SizedBox(height: 12),
|
||||
Text(message, textAlign: TextAlign.center),
|
||||
const SizedBox(height: 16),
|
||||
FilledButton.icon(
|
||||
onPressed: _reload,
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text('Tentar novamente'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final sales = snapshot.data ?? const <HistoricalSale>[];
|
||||
if (sales.isEmpty) {
|
||||
return RefreshIndicator(
|
||||
onRefresh: _reload,
|
||||
child: ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
children: const [
|
||||
SizedBox(height: 120),
|
||||
Icon(Icons.receipt_long_outlined, size: 56),
|
||||
SizedBox(height: 16),
|
||||
Center(child: Text('Nenhuma venda encontrada.')),
|
||||
SizedBox(height: 24),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: _reload,
|
||||
child: ListView.separated(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: sales.length,
|
||||
separatorBuilder: (_, __) => const SizedBox(height: 8),
|
||||
itemBuilder: (context, index) => _SaleCard(
|
||||
sale: sales[index],
|
||||
formatDate: _formatDate,
|
||||
onReprint: _confirmReprint,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _confirmReprint(HistoricalSale sale) async {
|
||||
final shouldReprint = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: const Text('Reimprimir NFC-e?'),
|
||||
content: Text(
|
||||
'Deseja reimprimir a nota do pedido ${sale.orderNumber}?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(false),
|
||||
child: const Text('Cancelar'),
|
||||
),
|
||||
FilledButton.icon(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(true),
|
||||
icon: const Icon(Icons.print_outlined),
|
||||
label: const Text('Reimprimir'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (shouldReprint != true || !mounted) return;
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
title: const Text('Impressão indisponível'),
|
||||
content: const Text(
|
||||
'A impressão da NFC-e ainda depende da integração com a API/SDK '
|
||||
'da impressora do POS.',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(dialogContext).pop(),
|
||||
child: const Text('Fechar'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SaleCard extends StatelessWidget {
|
||||
const _SaleCard({
|
||||
required this.sale,
|
||||
required this.formatDate,
|
||||
required this.onReprint,
|
||||
});
|
||||
|
||||
final HistoricalSale sale;
|
||||
final String Function(String) formatDate;
|
||||
final ValueChanged<HistoricalSale> onReprint;
|
||||
|
||||
Color _backgroundColor() => switch (sale.state) {
|
||||
'V' => const Color(0xFFE8F5E9),
|
||||
'F' => const Color(0xFFFFEBEE),
|
||||
_ => Colors.white,
|
||||
};
|
||||
|
||||
Color _stateColor() => switch (sale.state) {
|
||||
'V' => const Color(0xFF2E7D32),
|
||||
'F' => const Color(0xFFC62828),
|
||||
_ => Colors.black87,
|
||||
};
|
||||
|
||||
String _stateLabel() => switch (sale.state) {
|
||||
'V' => 'Validado',
|
||||
'F' => 'Faturado',
|
||||
_ => sale.state,
|
||||
};
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
color: _backgroundColor(),
|
||||
clipBehavior: Clip.antiAlias,
|
||||
child: InkWell(
|
||||
onTap: sale.state == 'F' ? () => onReprint(sale) : null,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
const Icon(Icons.receipt_long_outlined),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'Pedido ${sale.orderNumber}',
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
),
|
||||
Text(
|
||||
CurrencyFormatter.formatBrl(sale.totalMinorUnits),
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
_stateLabel(),
|
||||
style: TextStyle(
|
||||
color: _stateColor(),
|
||||
fontWeight: FontWeight.w700,
|
||||
),
|
||||
),
|
||||
if (sale.state == 'F') ...[
|
||||
const SizedBox(width: 6),
|
||||
Icon(Icons.print_outlined,
|
||||
size: 18, color: _stateColor()),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
Text('Emissão: ${formatDate(sale.issueDate)}'),
|
||||
if (sale.customerName.trim().isNotEmpty) ...[
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'Cliente: ${sale.customerName}',
|
||||
maxLines: 2,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user