import 'package:flutter/material.dart'; import '../../enumerations.dart'; import '../../services/auth/auth_service.dart'; import '../../services/user_service.dart'; import '../constants.dart'; import 'profile_category_form.dart'; import 'risks_form.dart'; class SectorsForm extends StatelessWidget { SectorsForm({ super.key, required this.isRegProcess, required this.isEditMode, }); final AuthService _authService = AuthService(); final bool isRegProcess; final bool isEditMode; @override Widget build(BuildContext context) { return FutureBuilder>( future: UserService.getSectorsFromFirebase( _authService.getCurrentUser()!.uid, ), builder: (context, snapshot) { if (snapshot.connectionState == ConnectionState.waiting) { return const CircularProgressIndicator(); } else if (snapshot.hasError) { return Text('Error: ${snapshot.error}'); } else { List? userSectors = snapshot.data; return ProfileCategoryForm( title: 'Sectors of interest', header: 'Sectors of interest', description: 'Select up to ${Constants.maxSectors} sectors that match your interests.', saveButtonText: isRegProcess ? 'Save and continue' : 'Save', options: SectorOption.values.toList(), // Convert enum to list minSelections: 1, maxSelections: Constants.maxSectors, preSelectedOptions: userSectors ?? [], onSave: (selectedOptions) async { // Handle saving selected options bool success = await UserService.saveSectorsToFirebase( selectedOptions.cast(), _authService.getCurrentUser()!.uid, ); if (context.mounted) { if (success) { if (isRegProcess) { Navigator.push( context, MaterialPageRoute( // set following registration page HERE builder: (context) => RisksFormPage( isRegProcess: isRegProcess, isEditMode: false, ), ), ); } else { if (isEditMode == true) { // pass selectedOptions data back to caller Navigator.pop(context, { Constants.dbFieldUsersSectors: selectedOptions, }); } else { // Navigate back after saving Navigator.pop(context); } } } else { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Failed to save sectors of interest.'), ), ); } } }, ); } }, ); } }