cofounderella/lib/forms/skills_form.dart

115 lines
4.1 KiB
Dart
Raw Permalink Normal View History

2024-05-15 13:35:01 +02:00
import 'package:flutter/material.dart';
import '../../enumerations.dart';
import '../../services/auth/auth_service.dart';
2024-05-20 01:16:54 +02:00
import '../../services/user_service.dart';
2024-05-31 18:31:37 +02:00
import '../constants.dart';
2024-05-15 13:35:01 +02:00
import 'profile_category_form.dart';
import 'sectors_form.dart';
2024-05-15 13:35:01 +02:00
class SkillsForm extends StatelessWidget {
2024-05-17 23:08:26 +02:00
SkillsForm({
super.key,
required this.isRegProcess,
required this.skillsSought,
2024-05-31 18:31:37 +02:00
required this.isEditMode,
2024-05-17 23:08:26 +02:00
});
2024-05-15 13:35:01 +02:00
final AuthService _authService = AuthService();
2024-05-17 23:08:26 +02:00
final bool isRegProcess;
2024-05-15 13:35:01 +02:00
final bool skillsSought; // flag to toggle offered and sought skills
2024-05-31 18:31:37 +02:00
final bool isEditMode;
2024-05-15 13:35:01 +02:00
@override
Widget build(BuildContext context) {
return FutureBuilder<List<SkillOption>>(
future: UserService.getSkillsFromFirebase(
2024-05-20 01:16:54 +02:00
skillsSought,
_authService.getCurrentUser()!.uid,
),
2024-05-15 13:35:01 +02:00
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
// Show loading indicator while fetching data
return const CircularProgressIndicator();
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
List<SkillOption>? userSkills = snapshot.data;
return ProfileCategoryForm(
title: 'Skills',
2024-05-20 01:16:54 +02:00
header: skillsSought
? 'Skills you are searching for'
: 'Your own skills',
2024-05-15 13:35:01 +02:00
description: skillsSought
? 'Choose up to ${Constants.maxSkills} areas you are looking for in a co-founder'
: 'Select up to ${Constants.maxSkills} areas in which you are skilled',
2024-05-17 23:08:26 +02:00
saveButtonText: isRegProcess ? 'Save and continue' : 'Save',
2024-05-15 13:35:01 +02:00
options: SkillOption.values.toList(), // Convert enum values to list
minSelections: 1,
maxSelections: Constants.maxSkills,
2024-05-15 13:35:01 +02:00
preSelectedOptions:
userSkills ?? [], // Pass pre-selected skills to the form
2024-05-17 23:08:26 +02:00
onSave: (selectedOptions) async {
2024-05-15 13:35:01 +02:00
// Handle saving selected options
bool success = await UserService.saveSkillsToFirebase(
2024-05-20 01:16:54 +02:00
selectedOptions.cast<SkillOption>(),
skillsSought,
_authService.getCurrentUser()!.uid,
);
2024-05-17 23:08:26 +02:00
// Then navigate to another screen or perform any other action
2024-05-17 23:08:26 +02:00
if (context.mounted) {
if (success) {
2024-05-20 01:16:54 +02:00
if (isRegProcess && skillsSought) {
Navigator.push(
context,
MaterialPageRoute(
2024-05-21 14:01:44 +02:00
// set following registration page HERE
builder: (context) => SectorsForm(
2024-05-31 23:16:15 +02:00
isRegProcess: isRegProcess,
isEditMode: false,
),
2024-05-17 23:08:26 +02:00
),
2024-05-20 01:16:54 +02:00
);
} else if (isRegProcess) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SkillsForm(
isRegProcess: isRegProcess,
skillsSought: true,
2024-05-31 18:31:37 +02:00
isEditMode: false,
2024-05-20 01:16:54 +02:00
),
),
);
} else {
2024-05-31 18:31:37 +02:00
if (isEditMode == true) {
// pass selectedOptions data back to caller
String keyToUpdate = skillsSought
? Constants.dbFieldUsersSkillsSought
: Constants.dbFieldUsersSkills;
Navigator.pop(context, {
keyToUpdate: selectedOptions,
});
} else {
// Navigate back after saving
Navigator.pop(context);
}
2024-05-20 01:16:54 +02:00
}
2024-05-17 23:08:26 +02:00
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Failed to save user skills.'),
),
);
}
}
2024-05-15 13:35:01 +02:00
},
);
}
},
);
}
}