payments_tab.dart 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import 'package:intl/intl.dart';
  5. import 'package:ristoris/providers/customer_provider.dart';
  6. class PaymentsTab extends ConsumerWidget {
  7. final int idCliente;
  8. final ThemeData theme;
  9. const PaymentsTab({required this.idCliente, required this.theme});
  10. @override
  11. Widget build(BuildContext context, WidgetRef ref) {
  12. final paysAsync = ref.watch(customerPaymentsProvider(idCliente));
  13. return paysAsync.when(
  14. data: (pays) {
  15. if (pays.isEmpty) return const Center(child: Text('Nessuna scadenza'));
  16. return ListView.builder(
  17. padding: const EdgeInsets.all(8),
  18. itemCount: pays.length,
  19. itemBuilder: (_, i) {
  20. final p = pays[i];
  21. return Card(
  22. margin: const EdgeInsets.symmetric(vertical: 4),
  23. child: Padding(
  24. padding: const EdgeInsets.all(12),
  25. child: Column(
  26. crossAxisAlignment: CrossAxisAlignment.start,
  27. children: [
  28. _row('Fattura N.', p.numDoc ?? '-'),
  29. _row('Data fatt.', _formatDate(p.dataDoc)),
  30. _row('Data scad', _formatDate(p.dataScadenza)),
  31. _row('Totale fattura', _formatAmount(p.importoFattura)),
  32. _row('Totale scad.', _formatAmount(p.importoScadenza)),
  33. _row('Saldo', _formatAmount(p.saldo)),
  34. ],
  35. ),
  36. ),
  37. );
  38. },
  39. );
  40. },
  41. loading: () => const Center(child: CircularProgressIndicator()),
  42. error: (e, _) => Center(child: Text('Errore: $e')),
  43. );
  44. }
  45. String _formatDate(String? dateStr) {
  46. if (dateStr == null) return '-';
  47. try {
  48. final dt = DateTime.parse(dateStr);
  49. return DateFormat('dd/MM/yyyy').format(dt);
  50. } catch (_) {
  51. return dateStr;
  52. }
  53. }
  54. String _formatAmount(double? amount) {
  55. if (amount == null) return '-';
  56. final fmt = NumberFormat('#,##0.00', 'it');
  57. return '€ ${fmt.format(amount)}';
  58. }
  59. Widget _row(String label, String value) {
  60. return Padding(
  61. padding: const EdgeInsets.symmetric(vertical: 2),
  62. child: Row(
  63. children: [
  64. SizedBox(
  65. width: 110,
  66. child: Text(
  67. label,
  68. style: theme.textTheme.bodySmall?.copyWith(
  69. color: theme.colorScheme.outline,
  70. ),
  71. ),
  72. ),
  73. Expanded(
  74. child: Text(value, style: theme.textTheme.bodyMedium),
  75. ),
  76. ],
  77. ),
  78. );
  79. }
  80. }