agent_switch_page.dart 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import 'package:ristoris/providers/auth_provider.dart';
  5. import 'package:ristoris/providers/database_provider.dart';
  6. class AgentSwitchPage extends ConsumerWidget {
  7. const AgentSwitchPage({super.key});
  8. @override
  9. Widget build(BuildContext context, WidgetRef ref) {
  10. final agentiAsync = ref.watch(agentiListProvider);
  11. final currentAgent = ref.watch(authProvider).agente;
  12. final theme = Theme.of(context);
  13. return Scaffold(
  14. appBar: AppBar(title: const Text('Cambia agente')),
  15. body: agentiAsync.when(
  16. data: (agenti) {
  17. final filtered =
  18. agenti.where((a) => a.idAgente != currentAgent?.idAgente).toList();
  19. if (filtered.isEmpty) {
  20. return const Center(child: Text('Nessun altro agente disponibile'));
  21. }
  22. return ListView.builder(
  23. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
  24. itemCount: filtered.length,
  25. itemBuilder: (_, i) {
  26. final a = filtered[i];
  27. return Card(
  28. margin: const EdgeInsets.only(bottom: 8),
  29. child: ListTile(
  30. leading: CircleAvatar(
  31. backgroundColor: theme.colorScheme.primaryContainer,
  32. child: Text(
  33. a.ragioneSociale?.substring(0, 1).toUpperCase() ?? '?',
  34. style: TextStyle(color: theme.colorScheme.onPrimaryContainer),
  35. ),
  36. ),
  37. title: Text(a.ragioneSociale ?? ''),
  38. subtitle: Text('Cod. ${a.codAgente ?? ''}'),
  39. trailing: const Icon(Icons.chevron_right),
  40. onTap: () async {
  41. await ref.read(authProvider.notifier).switchAgent(a);
  42. if (context.mounted) {
  43. context.go('/dashboard');
  44. }
  45. },
  46. ),
  47. );
  48. },
  49. );
  50. },
  51. loading: () => const Center(child: CircularProgressIndicator()),
  52. error: (e, _) => Center(child: Text('Errore: $e')),
  53. ),
  54. );
  55. }
  56. }