import 'package:flutter/material.dart'; import '../../enumerations.dart'; import '../../services/auth/auth_service.dart'; import '../../services/user_service.dart'; import '../constants.dart'; import '../pages/user_vision_page.dart'; import 'profile_category_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; int max = 10; return ProfileCategoryForm( title: 'Sectors of interest', header: 'Sectors of interest', description: 'Select up to $max sectors that match your interests.', saveButtonText: isRegProcess ? 'Save and continue' : 'Save', options: SectorOption.values.toList(), // Convert enum to list minSelections: 1, maxSelections: max, 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) => UserVisionPage( 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.'), ), ); } } }, ); } }, ); } }