Skip to content

Commit

Permalink
Fix multiple form issues
Browse files Browse the repository at this point in the history
  • Loading branch information
harveyjavier committed Nov 4, 2019
1 parent 5e4067d commit 7bcf81c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 50 deletions.
59 changes: 34 additions & 25 deletions lib/screens/edit_education.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,32 @@ class _EditEducationState extends State<EditEducation> {
start_year: education[i]["start_year"],
end_year: education[i]["end_year"] == null ? "" : education[i]["end_year"],
));
});
});
updateForm();
}
}
}

void updateForm() {
_forms.clear();
for (int i=0; i<_education.length; i++){
setState(() {
_forms.add(EducationForm(
key: GlobalKey(),
education: _education[i],
delete: () => deleteFields(i),
));
});
}
}

void addField() {
if (_canAdd) {
setState(() {
_canAdd = false;
_education.add(Education());
});
updateForm();
Future.delayed(Duration(milliseconds: 500)).then((onvalue) {
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
});
Expand All @@ -72,18 +87,11 @@ class _EditEducationState extends State<EditEducation> {

void deleteFields(int index) {
setState(() { _education.removeAt(index); });
updateForm();
}

@override
Widget build(BuildContext context) {
_forms.clear();
for (int i=0; i<_education.length; i++){
_forms.add(EducationForm(
key: GlobalKey(),
education: _education[i],
delete: () => deleteFields(i),
));
}
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
Expand Down Expand Up @@ -123,28 +131,29 @@ class _EditEducationState extends State<EditEducation> {
bool isValid = true;
List education = [];

Alert(
context: context,
title: "Saving...",
buttons: [
DialogButton(
onPressed: () {}, color: Colors.black,
child: SizedBox(
child: CircularProgressIndicator(
strokeWidth: 2.0, valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
height: 17.0, width: 17.0,
)
)
]
).show();

_forms.forEach((form) {
isValid = form.isValid();
print(form.data());
if (isValid) education.add(form.data());
});

if (isValid) {
Alert(
context: context,
title: "Saving...",
buttons: [
DialogButton(
onPressed: () {}, color: Colors.black,
child: SizedBox(
child: CircularProgressIndicator(
strokeWidth: 2.0, valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
height: 17.0, width: 17.0,
)
)
]
).show();

await db.collection("users").document(storage.getItem("user_data")["id"]).updateData({"education":education});
setState(() { storage.getItem("user_data")["education"] = education; });
Navigator.pop(context);
Expand Down
59 changes: 34 additions & 25 deletions lib/screens/edit_experience.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class _EditExperienceState extends State<EditExperience> {
start_year: experience[i]["start_year"],
end_year: experience[i]["end_year"] == null ? "" : experience[i]["end_year"],
));
});
});
updateForm();
}
}
}
Expand All @@ -53,12 +54,26 @@ class _EditExperienceState extends State<EditExperience> {
Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => Profile()));
}

void updateForm() {
_forms.clear();
for (int i=0; i<_experience.length; i++){
setState(() {
_forms.add(ExperienceForm(
key: GlobalKey(),
experience: _experience[i],
delete: () => deleteFields(i),
));
});
}
}

void addField() {
if (_canAdd) {
setState(() {
_canAdd = false;
_experience.add(Experience());
});
updateForm();
Future.delayed(Duration(milliseconds: 500)).then((onvalue) {
_scrollController.jumpTo(_scrollController.position.maxScrollExtent);
});
Expand All @@ -76,18 +91,11 @@ class _EditExperienceState extends State<EditExperience> {

void deleteFields(int index) {
setState(() { _experience.removeAt(index); });
updateForm();
}

@override
Widget build(BuildContext context) {
_forms.clear();
for (int i=0; i<_experience.length; i++){
_forms.add(ExperienceForm(
key: GlobalKey(),
experience: _experience[i],
delete: () => deleteFields(i),
));
}
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(
Expand Down Expand Up @@ -127,27 +135,28 @@ class _EditExperienceState extends State<EditExperience> {
bool isValid = true;
List experience = [];

Alert(
context: context,
title: "Saving...",
buttons: [
DialogButton(
onPressed: () {}, color: Colors.black,
child: SizedBox(
child: CircularProgressIndicator(
strokeWidth: 2.0, valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
height: 17.0, width: 17.0,
)
)
]
).show();

_forms.forEach((form) {
isValid = form.isValid();
if (isValid) experience.add(form.data());
});

if (isValid) {
Alert(
context: context,
title: "Saving...",
buttons: [
DialogButton(
onPressed: () {}, color: Colors.black,
child: SizedBox(
child: CircularProgressIndicator(
strokeWidth: 2.0, valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
),
height: 17.0, width: 17.0,
)
)
]
).show();

await db.collection("users").document(storage.getItem("user_data")["id"]).updateData({"experience":experience});
setState(() { storage.getItem("user_data")["experience"] = experience; });
Navigator.pop(context);
Expand Down

0 comments on commit 7bcf81c

Please sign in to comment.