passo_mobile_app/lib/screens/profile/components/education/add_modal.dart

543 lines
29 KiB
Dart
Raw Normal View History

2023-04-25 07:50:36 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:flutter_progress_hud/flutter_progress_hud.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:searchfield/searchfield.dart';
import 'package:unit2/bloc/profile/education/education_bloc.dart';
import 'package:unit2/model/profile/educational_background.dart';
import 'package:unit2/theme-data.dart/box_shadow.dart';
import 'package:unit2/theme-data.dart/form-style.dart';
import 'package:unit2/utils/global.dart';
import 'package:multi_dropdown/multiselect_dropdown.dart';
import '../../../../theme-data.dart/btn-style.dart';
import '../../../../theme-data.dart/colors.dart';
2023-06-06 06:54:51 +00:00
import '../../../../utils/formatters.dart';
2023-04-25 07:50:36 +00:00
import '../../../../utils/text_container.dart';
import '../../shared/add_for_empty_search.dart';
class AddEducationScreen extends StatefulWidget {
final int profileId;
final String token;
const AddEducationScreen(
{super.key, required this.profileId, required this.token});
@override
State<AddEducationScreen> createState() => _AddEducationScreenState();
}
class _AddEducationScreenState extends State<AddEducationScreen> {
final List<EducationLevel> educationLevel = [
const EducationLevel(type: "label", value: "Basic Education", group: 1),
const EducationLevel(type: "level", value: "Elementary", group: 1),
const EducationLevel(type: "level", value: "Secondary (non-K12)", group: 1),
const EducationLevel(type: "level", value: "Junior High", group: 1),
const EducationLevel(type: "level", value: "Senior High", group: 1),
const EducationLevel(type: "label", value: "Higher Education", group: 2),
const EducationLevel(type: "level", value: "Collegiate", group: 2),
const EducationLevel(type: "level", value: "Vocational", group: 2),
const EducationLevel(type: "label", value: "Post Graduate", group: 2),
const EducationLevel(type: "level", value: "Masteral", group: 2),
const EducationLevel(type: "level", value: "Doctorate", group: 2),
];
List<ValueItem> valueItemHonorList = [];
////selected
EducationLevel? selectedLevel;
School? selectedSchool;
Course? selectedProgram;
List<Honor> selectedHonors = [];
List<ValueItem>? selectedValueItem;
final formKey = GlobalKey<FormBuilderState>();
////congrollers
final addProgramController = TextEditingController();
final addSchoolController = TextEditingController();
final fromController = TextEditingController();
final untilController = TextEditingController();
final yearGraduated = TextEditingController();
////focus node
final programFocusNode = FocusNode();
final schoolFocusNode = FocusNode();
final honorFocusNode = FocusNode();
////booleans
bool graduated = true;
int? unitsEarned;
////
2023-06-06 06:54:51 +00:00
@override
void dispose() {
addProgramController.dispose();
addSchoolController.dispose();
fromController.dispose();
untilController.dispose();
2023-04-25 07:50:36 +00:00
2023-06-06 06:54:51 +00:00
programFocusNode.dispose();
schoolFocusNode.dispose();
honorFocusNode.dispose();
super.dispose();
}
2023-04-25 07:50:36 +00:00
@override
Widget build(BuildContext context) {
return BlocBuilder<EducationBloc, EducationState>(
builder: (context, state) {
if (state is AddEducationState) {
valueItemHonorList = state.honors.map((Honor honor) {
return ValueItem(label: honor.name!, value: honor.name);
}).toList();
return Container(
2023-06-06 06:54:51 +00:00
padding: const EdgeInsets.symmetric(vertical: 32, horizontal: 24),
2023-04-25 07:50:36 +00:00
child: FormBuilder(
key: formKey,
2023-06-06 06:54:51 +00:00
child: SizedBox(
height: blockSizeVertical * 85,
child: Column(
children: [
Flexible(
child: ListView(children: [
//// LEVEL
StatefulBuilder(builder: (context, setState) {
return Column(
children: [
FormBuilderDropdown(
validator: FormBuilderValidators.required(
errorText: "This field is required"),
decoration:
normalTextFieldStyle("level*", "level"),
name: "education_level",
onChanged: (EducationLevel? level) {
2023-04-25 07:50:36 +00:00
setState(() {
2023-06-06 06:54:51 +00:00
selectedLevel = level;
});
},
items: educationLevel
.map<DropdownMenuItem<EducationLevel>>(
(EducationLevel level) {
return level.type == "label"
? DropdownMenuItem(
enabled: false,
value: level,
child: Text(level.value.toUpperCase(),
style: const TextStyle(
color: Colors.black38)))
: DropdownMenuItem(
value: level,
enabled: true,
child: Text(
" ${level.value.toUpperCase()}"));
}).toList()),
const SizedBox(
height: 12,
),
////school
StatefulBuilder(builder: (context, setState) {
return SearchField(
inputFormatters: [UpperCaseTextFormatter()],
itemHeight: 70,
suggestionsDecoration: box1(),
suggestions: state.schools
.map((School school) =>
SearchFieldListItem(school.name!,
item: school,
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 10),
child: ListTile(
title: Text(school.name!,overflow: TextOverflow.visible,)),
)))
.toList(),
validator: (agency) {
if (agency!.isEmpty) {
return "This field is required";
}
return null;
},
focusNode: schoolFocusNode,
searchInputDecoration:
normalTextFieldStyle("School *", "").copyWith(
suffixIcon: IconButton(
icon: const Icon(Icons.arrow_drop_down),
onPressed: () {
schoolFocusNode.unfocus();
},
)),
onSuggestionTap: (school) {
setState(() {
selectedSchool = school.item;
schoolFocusNode.unfocus();
2023-04-25 07:50:36 +00:00
});
2023-06-06 06:54:51 +00:00
},
emptyWidget: EmptyWidget(
title: "Add School",
controller: addSchoolController,
onpressed: () {
2023-04-25 07:50:36 +00:00
setState(() {
2023-06-06 06:54:51 +00:00
School newSchool = School(
id: null,
name: addSchoolController.text
.toUpperCase());
state.schools.insert(0, newSchool);
addSchoolController.text = "";
Navigator.pop(context);
2023-04-25 07:50:36 +00:00
});
2023-06-06 06:54:51 +00:00
}),
);
}),
const SizedBox(
height: 12,
),
////Programs
Container(
child: selectedLevel != null &&
selectedLevel!.group != 1
? SearchField(
inputFormatters: [
UpperCaseTextFormatter()
],
itemHeight: 100,
suggestionsDecoration: box1(),
suggestions: state.programs
.map((Course program) =>
SearchFieldListItem(
program.program!,
item: program,
child: Padding(
padding: const EdgeInsets
.symmetric(
horizontal: 10),
child: ListTile(
title: Text(
program.program!,overflow: TextOverflow.visible,)),
)))
.toList(),
validator: (agency) {
if (agency!.isEmpty) {
return "This field is required";
}
return null;
},
focusNode: programFocusNode,
searchInputDecoration:
normalTextFieldStyle(
"Course/Programs *", "")
.copyWith(
suffixIcon: GestureDetector(
onTap: () => programFocusNode.unfocus(),
child: const Icon(
Icons.arrow_drop_down),
)),
onSuggestionTap: (position) {
2023-04-25 07:50:36 +00:00
setState(() {
2023-06-06 06:54:51 +00:00
selectedProgram = position.item;
programFocusNode.unfocus();
2023-04-25 07:50:36 +00:00
});
2023-06-06 06:54:51 +00:00
},
emptyWidget: EmptyWidget(
title: "Add Program",
controller: addProgramController,
onpressed: () {
setState(() {
Course newProgram = Course(
id: null,
program: addProgramController
.text
.toUpperCase());
state.programs
.insert(0, newProgram);
addProgramController.text = "";
2023-04-25 07:50:36 +00:00
Navigator.pop(context);
2023-06-06 06:54:51 +00:00
});
}),
)
: Container())
],
);
}),
const SizedBox(
height: 12,
),
StatefulBuilder(builder: (context, setState) {
return Column(
children: [
//// GRADUATED SWITCH
FormBuilderSwitch(
initialValue: graduated,
activeColor: second,
onChanged: (value) {
setState(() {
graduated = value!;
if (graduated) {
unitsEarned = null;
} else {
yearGraduated.text = "";
}
});
},
decoration: normalTextFieldStyle(
"Graduated?", 'Graduated?'),
name: 'graudated',
title: Text(graduated ? "YES" : "NO"),
),
const SizedBox(
height: 12,
),
////FROM
SizedBox(
width: screenWidth,
child: Row(
children: [
Flexible(
flex: 1,
child: FormBuilderTextField(
validator: FormBuilderValidators.required(
errorText: "This fied is required"),
decoration:
normalTextFieldStyle("from *", "from"),
name: "",
controller: fromController,
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: SizedBox(
width: 300,
height: 300,
child: YearPicker(
firstDate: DateTime(
DateTime.now().year - 100,
1),
lastDate: DateTime(
DateTime.now().year + 100,
1),
initialDate: DateTime.now(),
selectedDate: DateTime.now(),
onChanged: (DateTime dateTime) {
fromController.text =
dateTime.year.toString();
Navigator.pop(context);
},
),
),
);
},
2023-04-25 07:50:36 +00:00
);
},
2023-06-06 06:54:51 +00:00
),
),
const SizedBox(
width: 8,
),
////UNTIL
Flexible(
flex: 1,
child: FormBuilderTextField(
validator: FormBuilderValidators.required(
errorText: "This fied is required"),
decoration: normalTextFieldStyle(
"until *", "until"),
name: "",
controller: untilController,
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: SizedBox(
width: 300,
height: 300,
child: YearPicker(
firstDate: DateTime(
DateTime.now().year - 100,
1),
lastDate: DateTime(
DateTime.now().year + 100,
1),
initialDate: DateTime.now(),
selectedDate: DateTime.now(),
onChanged: (DateTime dateTime) {
untilController.text =
dateTime.year.toString();
Navigator.pop(context);
},
),
),
);
},
2023-04-25 07:50:36 +00:00
);
},
2023-06-06 06:54:51 +00:00
),
),
const SizedBox(
width: 8,
),
],
2023-04-25 07:50:36 +00:00
),
2023-06-06 06:54:51 +00:00
),
const SizedBox(
height: 12,
),
SizedBox(
child: graduated
////GRADUATED YEAR
? FormBuilderTextField(
validator: FormBuilderValidators.required(
errorText: "This fied is required"),
onTap: () {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: SizedBox(
width: 300,
height: 300,
child: YearPicker(
firstDate: DateTime(
DateTime.now().year - 100,
1),
lastDate: DateTime(
DateTime.now().year + 100,
1),
initialDate: DateTime.now(),
selectedDate: DateTime.now(),
onChanged: (DateTime dateTime) {
yearGraduated.text =
dateTime.year.toString();
Navigator.pop(context);
},
),
),
);
},
2023-04-25 07:50:36 +00:00
);
},
2023-06-06 06:54:51 +00:00
name: "year_graduated",
controller: yearGraduated,
decoration: normalTextFieldStyle(
"Year Graduated *", "Year Graduated *"),
)
//// HIGHEST UNITS EARNED
: FormBuilderTextField(
validator: FormBuilderValidators.required(
errorText: "This fied is required"),
name: "units_earned",
decoration: normalTextFieldStyle(
"Highest Level/Units Earned *",
"Highest Level/Units Earned *")),
),
],
);
}),
const SizedBox(
height: 12,
),
//// HONORS
MultiSelectDropDown(
onOptionSelected: (List<ValueItem> selectedOptions) {
selectedValueItem = selectedOptions;
2023-04-25 07:50:36 +00:00
},
2023-06-06 06:54:51 +00:00
borderColor: Colors.grey,
borderWidth: 1,
borderRadius: 5,
hint: "Honors",
padding: const EdgeInsets.all(8),
options: valueItemHonorList,
selectionType: SelectionType.multi,
chipConfig: const ChipConfig(wrapType: WrapType.wrap),
dropdownHeight: 300,
optionTextStyle: const TextStyle(fontSize: 16),
selectedOptionIcon: const Icon(Icons.check_circle),
),
const SizedBox(
height: 25,
),
////sumit button
const SizedBox(
height: 20,
),
]),
2023-04-25 07:50:36 +00:00
),
2023-06-06 06:54:51 +00:00
SizedBox(
width: double.infinity,
height: 60,
child: ElevatedButton(
style:
mainBtnStyle(primary, Colors.transparent, second),
onPressed: () {
if (formKey.currentState!.saveAndValidate()) {
//// program
if (selectedLevel!.value == "Elementary" ||
selectedLevel!.value ==
"Secondary (non-K12)" ||
selectedLevel!.value == "Junior High" ||
selectedLevel!.value == "Senior High") {
selectedProgram = null;
}
if (!graduated) {
unitsEarned = int.parse(formKey
.currentState!.value['units_earned']);
yearGraduated.text = '';
} else {}
////education
Education newEducation = Education(
id: null,
level: selectedLevel!.value,
course: selectedProgram,
school: selectedSchool);
////honors
if (selectedValueItem != null) {
for (var honor in selectedValueItem!) {
Honor newHonor = state.honors.firstWhere(
(element) => element.name == honor.value);
selectedHonors.add(newHonor);
}
}
EducationalBackground educationalBackground =
EducationalBackground(
id: null,
honors: null,
education: newEducation,
periodTo: untilController.text,
periodFrom: fromController.text,
yearGraduated:
graduated ? yearGraduated.text : null,
unitsEarned: !graduated ? unitsEarned : null,
attachments: null,
);
final progress = ProgressHUD.of(context);
progress!.showWithText("Loading...");
context.read<EducationBloc>().add(AddEducation(
educationalBackground: educationalBackground,
profileId: widget.profileId,
token: widget.token,
honors: selectedHonors));
}
},
child: const Text(submit)),
),
],
2023-04-25 07:50:36 +00:00
),
),
),
);
}
return Container();
},
);
}
}
class EducationLevel {
final String value;
final String type;
final int group;
const EducationLevel(
{required this.type, required this.value, required this.group});
}