diff --git a/lib/features/survey/presentation/contents/answer_content.dart b/lib/features/survey/presentation/contents/answer_content.dart index 05473f5..12badec 100644 --- a/lib/features/survey/presentation/contents/answer_content.dart +++ b/lib/features/survey/presentation/contents/answer_content.dart @@ -33,7 +33,7 @@ class _AnswerContentState extends State { late int secondsRemaining; late Timer timer; late ValueNotifier secondsNotifier; - Map responseJson = { + late Map responseJson = { 'survey_id': '', 'answers': [], }; @@ -51,6 +51,8 @@ class _AnswerContentState extends State { if (widget.questions.isNotEmpty) { selectedQuestion = widget.questions.first; + + _saveAnswerPoints(); } } @@ -320,37 +322,28 @@ class _AnswerContentState extends State { } } - void onFinishSurvey() { - _saveAnswerPoints(); - - Get.off( - () => ResultContent( - finaldata: responseJson, - ), - ); - } - void moveToNextQuestion() { final currentIndex = widget.questions.indexOf(selectedQuestion!); + if (selectedQuestion!.scoring) { + _saveAnswerPoints(); + } + if (currentIndex == widget.questions.length - 1 && secondsRemaining <= 0) { if (selectedQuestion!.scoring) { - _saveAnswerPoints(); Get.off( () => ResultContent( finaldata: responseJson, ), ); } - } else if (currentIndex < widget.questions.length - 1) { + } else { setState(() { - selectedQuestion = widget.questions[currentIndex + 1]; - secondsRemaining = 10; - selectedRadioValue = null; - selectedRadioValues.clear(); - - if (selectedQuestion!.scoring) { - _saveAnswerPoints(); + if (currentIndex < widget.questions.length - 1) { + selectedQuestion = widget.questions[currentIndex + 1]; + secondsRemaining = 10; + selectedRadioValue = null; + selectedRadioValues.clear(); } }); } @@ -384,6 +377,7 @@ class _AnswerContentState extends State { }); } } + responseJson = { 'survey_id': widget.surveyId, 'answers': answerList, @@ -391,4 +385,12 @@ class _AnswerContentState extends State { print('check hasil akhir $responseJson'); } + + void onFinishSurvey() { + Get.off( + () => ResultContent( + finaldata: responseJson, + ), + ); + } } diff --git a/lib/features/survey/presentation/contents/results_content.dart b/lib/features/survey/presentation/contents/results_content.dart index 3d39612..6b7384f 100644 --- a/lib/features/survey/presentation/contents/results_content.dart +++ b/lib/features/survey/presentation/contents/results_content.dart @@ -15,9 +15,11 @@ class ResultContent extends StatelessWidget { Widget build(BuildContext context) { final finalDataModel = FinalDataModel.fromJson(finaldata); final answers = finalDataModel.answers; - var score = '0'; + + var score = 0; + for (final answer in answers) { - score = answer.answer; + score += int.tryParse(answer.answer) ?? 0; } return Scaffold( @@ -34,7 +36,7 @@ class ResultContent extends StatelessWidget { ), Expanded( child: Text( - score, + score.toString(), // Display the total score style: StyleTypograph.heading1.bold, ), ), diff --git a/lib/features/survey/presentation/widgets/popup_widget.dart b/lib/features/survey/presentation/widgets/popup_widget.dart index 7b7f2e5..09aef8f 100644 --- a/lib/features/survey/presentation/widgets/popup_widget.dart +++ b/lib/features/survey/presentation/widgets/popup_widget.dart @@ -17,6 +17,8 @@ class ShowPopup extends StatefulWidget { } class _ShowPopupState extends State { + late int selectedQuestionIndex = 0; + @override Widget build(BuildContext context) { final data = widget.data; @@ -38,8 +40,12 @@ class _ShowPopupState extends State { itemCount: data.length, itemBuilder: (context, index) { final question = data[index]; + return GestureDetector( onTap: () { + setState(() { + selectedQuestionIndex = index; + }); widget.onItemSelected(question); Navigator.of(context).pop(); }, @@ -47,14 +53,18 @@ class _ShowPopupState extends State { padding: const EdgeInsets.all(20), decoration: BoxDecoration( border: Border.all(), - color: Colors.blue, + color: index == selectedQuestionIndex + ? Colors.blue + : Colors.white, borderRadius: BorderRadius.circular(10), ), child: Center( child: Text( question.number, style: StyleTypograph.label3.bold.copyWith( - color: Colors.white, + color: index == selectedQuestionIndex + ? Colors.white + : Colors.black, decoration: TextDecoration.none, ), ), diff --git a/lib/features/survey/presentation/widgets/survey_button_widget.dart b/lib/features/survey/presentation/widgets/survey_button_widget.dart index d8a0be6..9ef87f3 100644 --- a/lib/features/survey/presentation/widgets/survey_button_widget.dart +++ b/lib/features/survey/presentation/widgets/survey_button_widget.dart @@ -2,7 +2,7 @@ import 'package:flutter/material.dart'; import 'package:gap/gap.dart'; import 'package:ristu_intern_challenge/core/shared/widgets/custom_button_widget.dart'; -class SurveyButtonWidget extends StatelessWidget { +class SurveyButtonWidget extends StatefulWidget { const SurveyButtonWidget({ required this.onNextPressed, required this.onBackPressed, @@ -10,11 +10,19 @@ class SurveyButtonWidget extends StatelessWidget { required this.isLastQuestion, super.key, }); + final VoidCallback onNextPressed; final VoidCallback onBackPressed; final VoidCallback onFinishPressed; final bool isLastQuestion; + @override + _SurveyButtonWidgetState createState() => _SurveyButtonWidgetState(); +} + +class _SurveyButtonWidgetState extends State { + bool isBackButtonPressed = false; + @override Widget build(BuildContext context) { return Padding( @@ -26,16 +34,25 @@ class SurveyButtonWidget extends StatelessWidget { buttonColor: Colors.white, text: 'Back', textColor: Colors.cyan, - onPress: onBackPressed, + onPress: () { + if (!isBackButtonPressed) { + setState(() { + isBackButtonPressed = true; + }); + widget.onBackPressed(); + } + }, ), ), const Gap(16), Expanded( child: CustomButtonWidget( buttonColor: Colors.cyan, - text: isLastQuestion ? 'Finish' : 'Next', + text: widget.isLastQuestion ? 'Finish' : 'Next', textColor: Colors.white, - onPress: isLastQuestion ? onFinishPressed : onNextPressed, + onPress: widget.isLastQuestion + ? widget.onFinishPressed + : widget.onNextPressed, ), ), ],