cofounderella/lib/forms/risks_form.dart

159 lines
4.9 KiB
Dart

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import '../constants.dart';
import '../enumerations.dart';
import '../pages/registration_complete_page.dart';
import '../services/auth/auth_service.dart';
class RisksFormPage extends StatefulWidget {
const RisksFormPage({super.key, required this.isRegProcess});
final bool isRegProcess;
@override
RisksFormPageState createState() => RisksFormPageState();
}
class RisksFormPageState extends State<RisksFormPage> {
CommunicationPreference? communicationPreference;
RiskTolerance? riskTolerance;
final AuthService _authService = AuthService();
@override
void initState() {
super.initState();
_loadDataFromFirebase();
}
Future<void> _loadDataFromFirebase() async {
final userDoc = FirebaseFirestore.instance
.collection(Constants.dbCollectionUsers)
.doc(_authService.getCurrentUser()!.uid);
final snapshot = await userDoc.get();
if (snapshot.exists) {
final data = snapshot.data();
setState(() {
if (data != null) {
// Load Communication Preference
if (data[Constants.dbFieldUsersCommunication] != null) {
communicationPreference = CommunicationPreference.values
.firstWhereOrNull((x) =>
x.toString() == data[Constants.dbFieldUsersCommunication]);
}
// Load Risk Tolerance
if (data[Constants.dbFieldUsersRiskTolerance] != null) {
riskTolerance = RiskTolerance.values.firstWhereOrNull((x) =>
x.toString() == data[Constants.dbFieldUsersRiskTolerance]);
}
}
});
}
}
Future<void> _saveDataToFirebase() async {
final userDoc = FirebaseFirestore.instance
.collection(Constants.dbCollectionUsers)
.doc(_authService.getCurrentUser()!.uid);
await userDoc.set(
{
Constants.dbFieldUsersCommunication:
communicationPreference?.toString(),
Constants.dbFieldUsersRiskTolerance: riskTolerance?.toString(),
},
SetOptions(merge: true), // avoid overwriting existing data
);
}
void handleSubmit() {
if (communicationPreference == null) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Please select a communication preference.'),
));
return;
}
if (riskTolerance == null) {
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Please select the willingness to take risks.'),
));
return;
}
_saveDataToFirebase();
// Handle the form submission logic here
Navigator.push(
context,
MaterialPageRoute(
//
// TODO set following registration page HERE
//
builder: (context) => const RegistrationCompletePage(),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Personal preferences'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Communication preference',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const Text('How do you prefer to communicate in a team?'),
...CommunicationPreference.values.map((option) {
return RadioListTile(
title: Text(option.displayName),
value: option,
groupValue: communicationPreference,
onChanged: (CommunicationPreference? value) {
setState(() {
communicationPreference = value;
});
},
);
}),
const SizedBox(height: 40),
const Text(
'Risk tolerance',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const Text('How do you deal with uncertainty and risk?'),
...RiskTolerance.values.map((option) {
return RadioListTile(
title: Text(option.displayName),
value: option,
groupValue: riskTolerance,
onChanged: (RiskTolerance? value) {
setState(() {
riskTolerance = value;
});
},
);
}),
const SizedBox(height: 20),
Center(
child: ElevatedButton(
onPressed: handleSubmit,
child:
Text(widget.isRegProcess ? 'Save and continue' : 'Save'),
),
),
],
),
),
),
);
}
}