Skip to content

Commit

Permalink
aku wes loss
Browse files Browse the repository at this point in the history
  • Loading branch information
crosbydoo committed Dec 14, 2023
1 parent 52be69c commit 8a02ecf
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 29 deletions.
42 changes: 22 additions & 20 deletions lib/features/survey/presentation/contents/answer_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class _AnswerContentState extends State<AnswerContent> {
late int secondsRemaining;
late Timer timer;
late ValueNotifier<int> secondsNotifier;
Map<String, dynamic> responseJson = {
late Map<String, dynamic> responseJson = {
'survey_id': '',
'answers': [],
};
Expand All @@ -51,6 +51,8 @@ class _AnswerContentState extends State<AnswerContent> {

if (widget.questions.isNotEmpty) {
selectedQuestion = widget.questions.first;

_saveAnswerPoints();
}
}

Expand Down Expand Up @@ -320,37 +322,28 @@ class _AnswerContentState extends State<AnswerContent> {
}
}

void onFinishSurvey() {
_saveAnswerPoints();

Get.off<void>(
() => 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<void>(
() => 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();
}
});
}
Expand Down Expand Up @@ -384,11 +377,20 @@ class _AnswerContentState extends State<AnswerContent> {
});
}
}

responseJson = <String, dynamic>{
'survey_id': widget.surveyId,
'answers': answerList,
};

print('check hasil akhir $responseJson');
}

void onFinishSurvey() {
Get.off<void>(
() => ResultContent(
finaldata: responseJson,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -34,7 +36,7 @@ class ResultContent extends StatelessWidget {
),
Expanded(
child: Text(
score,
score.toString(), // Display the total score
style: StyleTypograph.heading1.bold,
),
),
Expand Down
14 changes: 12 additions & 2 deletions lib/features/survey/presentation/widgets/popup_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class ShowPopup extends StatefulWidget {
}

class _ShowPopupState extends State<ShowPopup> {
late int selectedQuestionIndex = 0;

@override
Widget build(BuildContext context) {
final data = widget.data;
Expand All @@ -38,23 +40,31 @@ class _ShowPopupState extends State<ShowPopup> {
itemCount: data.length,
itemBuilder: (context, index) {
final question = data[index];

return GestureDetector(
onTap: () {
setState(() {
selectedQuestionIndex = index;
});
widget.onItemSelected(question);
Navigator.of(context).pop();
},
child: Container(
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,
),
),
Expand Down
25 changes: 21 additions & 4 deletions lib/features/survey/presentation/widgets/survey_button_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@ 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,
required this.onFinishPressed,
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<SurveyButtonWidget> {
bool isBackButtonPressed = false;

@override
Widget build(BuildContext context) {
return Padding(
Expand All @@ -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,
),
),
],
Expand Down

0 comments on commit 8a02ecf

Please sign in to comment.