Skip to content

Commit e436080

Browse files
committed
fix: form submisison
1 parent fe74969 commit e436080

File tree

3 files changed

+141
-86
lines changed

3 files changed

+141
-86
lines changed

lib/core/model/response_models/post_form_success_model.dart

+65-5
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,83 @@ import 'package:flutter/foundation.dart';
33

44
class PostFormSuccessModel {
55
String? message;
6+
SubmissionModel? submission;
7+
8+
PostFormSuccessModel({
9+
this.message,
10+
this.submission,
11+
});
12+
13+
static const String kMessage = "message";
14+
static const String kSubmission = "submission";
15+
16+
PostFormSuccessModel.fromJson(Map<String, dynamic> json) {
17+
message = json[kMessage] ?? TextConstants.formSubmittedSuccessfully;
18+
submission = json[kSubmission] != null
19+
? SubmissionModel.fromJson(json[kSubmission])
20+
: null;
21+
}
22+
23+
String getMessage() {
24+
if (submission?.form?.formName == null) {
25+
return message ?? TextConstants.formSubmittedSuccessfully;
26+
}
27+
28+
final formattedDate = submission?.submittedDate != null
29+
? "${submission!.submittedDate!.day}-${submission!.submittedDate!.month}-${submission!.submittedDate!.year}"
30+
: "today";
31+
32+
return "Successfully submitted ${submission!.form!.formName} on $formattedDate\nSubmission ID: ${submission?.submissionId?.substring(0, 8) ?? 'N/A'}";
33+
}
34+
}
35+
36+
class SubmissionModel {
37+
FormInfoModel? form;
38+
String? formId;
639
String? submissionId;
740
DateTime? submittedDate;
841

9-
PostFormSuccessModel({this.message, this.submissionId, this.submittedDate});
42+
SubmissionModel({
43+
this.form,
44+
this.formId,
45+
this.submissionId,
46+
this.submittedDate,
47+
});
1048

11-
static const String kMessage = "message";
49+
static const String kForm = "form";
50+
static const String kFormId = "formId";
1251
static const String kSubmissionId = "submissionId";
1352
static const String kSubmittedDate = "submittedDate";
1453

15-
PostFormSuccessModel.fromJson(Map<String, dynamic> json) {
16-
message = json[kMessage] ?? TextConstants.formSubmittedSuccessfully;
54+
SubmissionModel.fromJson(Map<String, dynamic> json) {
55+
form = json[kForm] != null ? FormInfoModel.fromJson(json[kForm]) : null;
56+
formId = json[kFormId];
1757
submissionId = json[kSubmissionId];
1858
try {
19-
submittedDate = DateTime.parse(json[kSubmittedDate]);
59+
submittedDate = json[kSubmittedDate] != null
60+
? DateTime.parse(json[kSubmittedDate])
61+
: null;
2062
} catch (e) {
2163
debugPrint("Error parsing submittedDate: $e");
2264
submittedDate = null;
2365
}
2466
}
2567
}
68+
69+
class FormInfoModel {
70+
String? formId;
71+
String? formName;
72+
73+
FormInfoModel({
74+
this.formId,
75+
this.formName,
76+
});
77+
78+
static const String kFormId = "formId";
79+
static const String kFormName = "formName";
80+
81+
FormInfoModel.fromJson(Map<String, dynamic> json) {
82+
formId = json[kFormId];
83+
formName = json[kFormName];
84+
}
85+
}

lib/presentation/screens/form_screen.dart

+16-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class _DynamicFormScreenState extends State<DynamicFormScreen> {
2727

2828
@override
2929
void initState() {
30+
// Clear previous form data when starting a new form
31+
ViewFormDataScreen.dynamicFormFilledValues.clear();
32+
3033
for (int i = 0; i < widget.formModel.pages!.length; i++) {
3134
pageFormKeys.add(GlobalKey<FormState>());
3235

@@ -56,9 +59,15 @@ class _DynamicFormScreenState extends State<DynamicFormScreen> {
5659
return BlocListener<PostFormCubit, PostFormState>(
5760
listener: (context, postFormState) {
5861
if (postFormState is PostFormSuccessState) {
59-
debugPrint("postFormSuccess: ${postFormState.postFormSuccessModel}");
62+
debugPrint(
63+
"postFormSuccess: ${postFormState.postFormSuccessModel.getMessage()}");
64+
6065
Navigator.of(context).pushNamedAndRemoveUntil(
6166
AppRoutes.listOfFormsScreen, (route) => false);
67+
showFlushBar(
68+
context,
69+
postFormState.postFormSuccessModel.getMessage(),
70+
);
6271
} else if (postFormState is PostFormFailureState) {
6372
debugPrint("postFormFailure: ${postFormState.failure}");
6473
showFlushBar(context, postFormState.failure.getErrorMsg());
@@ -120,26 +129,24 @@ class _DynamicFormScreenState extends State<DynamicFormScreen> {
120129
if (pageFormKeys[pageIndex]
121130
.currentState!
122131
.validate()) {
132+
// Save current page data
123133
Map<String, dynamic> thisPageFilledData =
124134
formModel.pages![pageIndex].toJson();
125135
debugPrint(
126136
"page filled data: $thisPageFilledData");
127-
for (var field in thisPageFilledData.entries) {
128-
ViewFormDataScreen
129-
.dynamicFormFilledValues[field.key] =
130-
field.value;
131-
}
137+
138+
// Update the form values map with current page data
139+
ViewFormDataScreen.dynamicFormFilledValues
140+
.addAll(thisPageFilledData);
132141

133142
if (isAtLastPage(pageAt: pageIndex)) {
134143
debugPrint("page validated and at last page");
144+
// Now we have all pages' data accumulated
135145
BlocProvider.of<PostFormCubit>(context)
136146
.postForm(
137147
formData: ViewFormDataScreen
138148
.dynamicFormFilledValues,
139149
);
140-
// Navigator.of(context).pushNamedAndRemoveUntil(
141-
// AppRoutes.listOfFormsScreen,
142-
// (route) => false);
143150
} else {
144151
goNextPage();
145152
}

lib/presentation/screens/list_of_forms_screen.dart

+60-72
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import 'package:dynamicform/business_logic/get_forms_cubit/get_forms_cubit.dart';
22
import 'package:dynamicform/business_logic/get_forms_cubit/get_forms_state.dart';
33
import 'package:dynamicform/business_logic/post_form_cubit/post_form_cubit.dart';
4-
import 'package:dynamicform/business_logic/post_form_cubit/post_form_state.dart';
54
import 'package:dynamicform/core/constants/text_constants.dart';
65
import 'package:dynamicform/core/model/form_model.dart';
76
import 'package:dynamicform/core/routes/app_routes.dart';
8-
import 'package:dynamicform/core/utils/show_flushbar.dart';
97
import 'package:dynamicform/presentation/widgets/custom_text_widget.dart';
108
import 'package:flutter/material.dart';
119
import 'package:flutter_bloc/flutter_bloc.dart';
@@ -30,79 +28,69 @@ class _ListOfFormsScreenState extends State<ListOfFormsScreen> {
3028

3129
@override
3230
Widget build(BuildContext context) {
33-
return BlocListener<PostFormCubit, PostFormState>(
34-
listener: (context, postFormState) {
35-
if (postFormState is PostFormSuccessState) {
36-
showFlushBar(
37-
context,
38-
postFormState.postFormSuccessModel.message ?? TextConstants.nA,
39-
);
40-
}
41-
},
42-
child: Scaffold(
43-
appBar: AppBar(
44-
title: const CustomText(text: TextConstants.formTemplates),
45-
),
46-
body: RefreshIndicator(
47-
onRefresh: _onRefresh,
48-
child: BlocBuilder<GetFormsCubit, GetFormsState>(
49-
builder: (context, state) {
50-
if (state is GetFormsLoadingState) {
51-
return const Center(child: CircularProgressIndicator());
52-
} else if (state is GetFormsFailureState) {
53-
return Center(
54-
child: SingleChildScrollView(
55-
physics: const AlwaysScrollableScrollPhysics(),
56-
child: Container(
57-
height: MediaQuery.of(context).size.height - 100,
58-
alignment: Alignment.center,
59-
child: CustomText(text: state.failure.getErrorMsg()),
60-
),
31+
return Scaffold(
32+
appBar: AppBar(
33+
title: const CustomText(text: TextConstants.formTemplates),
34+
),
35+
body: RefreshIndicator(
36+
onRefresh: _onRefresh,
37+
child: BlocBuilder<GetFormsCubit, GetFormsState>(
38+
builder: (context, state) {
39+
if (state is GetFormsLoadingState) {
40+
return const Center(child: CircularProgressIndicator());
41+
} else if (state is GetFormsFailureState) {
42+
return Center(
43+
child: SingleChildScrollView(
44+
physics: const AlwaysScrollableScrollPhysics(),
45+
child: Container(
46+
height: MediaQuery.of(context).size.height - 100,
47+
alignment: Alignment.center,
48+
child: CustomText(text: state.failure.getErrorMsg()),
6149
),
62-
);
63-
} else if (state is GetFormsSuccessState) {
64-
List<DynamicFormModel> forms = state.forms.forms ?? [];
65-
return forms.isEmpty
66-
? const Center(
67-
child: CustomText(
68-
text: TextConstants.noFormsAvailable,
69-
),
70-
)
71-
: ListView.builder(
72-
physics: const AlwaysScrollableScrollPhysics(),
73-
itemCount: forms.length,
74-
itemBuilder: (context, index) {
75-
return ListTile(
76-
title: CustomText(
77-
text: forms[index].formName ?? TextConstants.nA,
78-
),
79-
leading: CustomText(
80-
text: "${index + 1}.",
81-
),
82-
onTap: () {
83-
BlocProvider.of<PostFormCubit>(context)
84-
.formIdInDB = forms[index].formId;
85-
Navigator.pushNamed(
86-
context, AppRoutes.fillFormScreen,
87-
arguments: forms[index]);
88-
},
89-
trailing:
90-
const Icon(Icons.arrow_forward_ios, size: 18),
91-
);
92-
},
93-
);
94-
}
95-
96-
return SingleChildScrollView(
97-
physics: const AlwaysScrollableScrollPhysics(),
98-
child: Container(
99-
height: MediaQuery.of(context).size.height - 100,
100-
alignment: Alignment.center,
101-
child: const CustomText(text: TextConstants.noFormsAvailable),
10250
),
10351
);
104-
},
105-
),
52+
} else if (state is GetFormsSuccessState) {
53+
List<DynamicFormModel> forms = state.forms.forms ?? [];
54+
return forms.isEmpty
55+
? const Center(
56+
child: CustomText(
57+
text: TextConstants.noFormsAvailable,
58+
),
59+
)
60+
: ListView.builder(
61+
physics: const AlwaysScrollableScrollPhysics(),
62+
itemCount: forms.length,
63+
itemBuilder: (context, index) {
64+
return ListTile(
65+
title: CustomText(
66+
text: forms[index].formName ?? TextConstants.nA,
67+
),
68+
leading: CustomText(
69+
text: "${index + 1}.",
70+
),
71+
onTap: () {
72+
BlocProvider.of<PostFormCubit>(context).formIdInDB =
73+
forms[index].formId;
74+
Navigator.pushNamed(
75+
context, AppRoutes.fillFormScreen,
76+
arguments: forms[index]);
77+
},
78+
trailing:
79+
const Icon(Icons.arrow_forward_ios, size: 18),
80+
);
81+
},
82+
);
83+
}
84+
85+
return SingleChildScrollView(
86+
physics: const AlwaysScrollableScrollPhysics(),
87+
child: Container(
88+
height: MediaQuery.of(context).size.height - 100,
89+
alignment: Alignment.center,
90+
child: const CustomText(text: TextConstants.noFormsAvailable),
91+
),
92+
);
93+
},
10694
),
10795
),
10896
);

0 commit comments

Comments
 (0)