Edit Skills.

master
Rafael 2024-05-31 18:31:37 +02:00
parent d246829784
commit 3aa5f2b5d3
5 changed files with 159 additions and 15 deletions

View File

@ -2,6 +2,7 @@ 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';
@ -10,6 +11,7 @@ class SkillsForm extends StatelessWidget {
super.key,
required this.isRegProcess,
required this.skillsSought,
required this.isEditMode,
});
// get instance of auth
@ -17,6 +19,7 @@ class SkillsForm extends StatelessWidget {
final bool isRegProcess;
final bool skillsSought; // flag to toggle offered and sought skills
final bool isEditMode;
@override
Widget build(BuildContext context) {
@ -74,12 +77,24 @@ class SkillsForm extends StatelessWidget {
builder: (context) => SkillsForm(
isRegProcess: isRegProcess,
skillsSought: true,
isEditMode: false,
),
),
);
} else {
// Navigate back after saving
Navigator.pop(context);
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);
}
}
} else {
ScaffoldMessenger.of(context).showSnackBar(

View File

@ -1,4 +1,5 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:cofounderella/services/user_service.dart';
import '../constants.dart';
import '../enumerations.dart';
@ -17,8 +18,8 @@ class UserProfile {
Gender? gender;
int? born;
final String risk;
final List<String> skills;
final List<String> skillsSought;
List<SkillOption> skills;
List<SkillOption> skillsSought;
List<Language> languages;
Map<String, MyLocation?> locations;
@ -43,6 +44,11 @@ class UserProfile {
factory UserProfile.fromDocument(DocumentSnapshot doc) {
Map<String, dynamic> data = doc.data() as Map<String, dynamic>;
List<SkillOption> skillsOffered = UserService.convertSkillStringToEnum(
data[Constants.dbFieldUsersSkills]);
List<SkillOption> skillsSought = UserService.convertSkillStringToEnum(
data[Constants.dbFieldUsersSkillsSought]);
return UserProfile(
id: doc.id,
uid: data[Constants.dbFieldUsersID] ?? '',
@ -50,9 +56,8 @@ class UserProfile {
name: data[Constants.dbFieldUsersName] ?? '',
firstName: data[Constants.dbFieldUsersFirstName] ?? '',
lastName: data[Constants.dbFieldUsersLastName] ?? '',
skills: List<String>.from(data[Constants.dbFieldUsersSkills] ?? []),
skillsSought:
List<String>.from(data[Constants.dbFieldUsersSkillsSought] ?? []),
skills: skillsOffered,
skillsSought: skillsSought,
risk: data[Constants.dbFieldUsersRiskTolerance] ?? '',
profilePictureUrl: data[Constants.dbFieldUsersProfilePic],
bio: data[Constants.dbFieldUsersBio],

View File

@ -329,6 +329,7 @@ class _UserDataPageState extends State<UserDataPage> {
builder: (context) => SkillsForm(
isRegProcess: widget.isRegProcess,
skillsSought: false,
isEditMode: false,
),
),
);

View File

@ -1,3 +1,5 @@
import 'package:cofounderella/enumerations.dart';
import 'package:cofounderella/forms/skills_form.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
@ -80,6 +82,43 @@ class _UserProfilePageState extends State<UserProfilePage> {
}
}
void _editSkills({required bool skillsSought}) async {
final updatedUserData = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SkillsForm(
isRegProcess: false,
skillsSought: skillsSought,
isEditMode: true,
),
),
);
if (updatedUserData != null) {
setState(() {
// above Type of updatedUserData is dynamic, so convert
if (skillsSought) {
List<dynamic> dynamicList =
updatedUserData[Constants.dbFieldUsersSkillsSought];
myData.skillsSought =
dynamicList.map((e) => e as SkillOption).toList();
} else {
List<dynamic> dynamicList =
updatedUserData[Constants.dbFieldUsersSkills];
myData.skills = dynamicList.map((e) => e as SkillOption).toList();
}
});
}
}
void editUserSkillsInfo() async {
_editSkills(skillsSought: false);
}
void editUserSkillsSoughtInfo() async {
_editSkills(skillsSought: true);
}
@override
Widget build(BuildContext context) {
return Scaffold(
@ -101,6 +140,10 @@ class _UserProfilePageState extends State<UserProfilePage> {
const SizedBox(height: 16),
Divider(color: Theme.of(context).colorScheme.primary),
const SizedBox(height: 16),
_buildSkills(context),
const SizedBox(height: 16),
Divider(color: Theme.of(context).colorScheme.primary),
const SizedBox(height: 16),
],
),
),
@ -108,6 +151,81 @@ class _UserProfilePageState extends State<UserProfilePage> {
);
}
Widget _buildSkills(BuildContext context) {
return Column(
children: [
Align(
alignment: Alignment.centerLeft,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Skills offered',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
),
Text(myData.skills.map((x) => x.displayName).join(', '),
style: const TextStyle(fontSize: 16)),
],
),
),
Align(
alignment: Alignment.topRight,
child: OutlinedButton.icon(
label: const Text('Edit'),
icon: const Icon(Icons.edit),
onPressed: editUserSkillsInfo,
),
),
],
),
const SizedBox(height: 16),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Skills sought',
style: TextStyle(
color: Theme.of(context).colorScheme.primary,
),
),
Text(
myData.skillsSought
.map((x) => x.displayName)
.join(', '),
style: const TextStyle(fontSize: 16)),
],
),
),
Align(
alignment: Alignment.topRight,
child: OutlinedButton.icon(
label: const Text('Edit'),
icon: const Icon(Icons.edit),
onPressed: editUserSkillsSoughtInfo,
),
),
],
),
],
),
),
],
);
}
Widget _buildLocation(BuildContext context) {
int age = calcAge(myData.born);
return Column(

View File

@ -51,19 +51,24 @@ class UserService {
skills = userData[Constants.dbFieldUsersSkills];
}
if (skills != null && skills.isNotEmpty) {
// Convert skills from strings to enum values
List<SkillOption> userSkills = skills
.map((skill) => SkillOption.values
.firstWhere((x) => x.toString() == 'SkillOption.$skill'))
.toList();
return userSkills;
}
return convertSkillStringToEnum(skills);
}
return [];
}
static List<SkillOption> convertSkillStringToEnum(List<dynamic>? skills) {
if (skills != null && skills.isNotEmpty) {
// Convert skills from strings to enum values
List<SkillOption> userSkills = skills
.map((skill) => SkillOption.values
.firstWhere((x) => x.toString() == 'SkillOption.$skill'))
.toList();
return userSkills;
}
return [];
}
static Future<bool> saveSkillsToFirebase(List<SkillOption> selectedOptions,
bool skillsSought, String userId) async {
try {