From 5aaeb2f4344d7cb3510ceffd844223ff899c9553 Mon Sep 17 00:00:00 2001 From: dandiindra29 Date: Fri, 10 Nov 2023 07:38:59 +0700 Subject: [PATCH] update answer input --- .../bloc/quiz_exercise_state.dart | 1 + .../pages/quiz_exercise_page.dart | 163 +++++++++++------- 2 files changed, 100 insertions(+), 64 deletions(-) diff --git a/app/lib/features/quiz_exercise/presentation/bloc/quiz_exercise_state.dart b/app/lib/features/quiz_exercise/presentation/bloc/quiz_exercise_state.dart index 9de3074..0a2a799 100644 --- a/app/lib/features/quiz_exercise/presentation/bloc/quiz_exercise_state.dart +++ b/app/lib/features/quiz_exercise/presentation/bloc/quiz_exercise_state.dart @@ -33,6 +33,7 @@ class QuizExerciseShow extends QuizExerciseState { quizExercise, remainingDuration, selectedAnswer, + shortAnswer, modalErrorMessage, ]; } diff --git a/app/lib/features/quiz_exercise/presentation/pages/quiz_exercise_page.dart b/app/lib/features/quiz_exercise/presentation/pages/quiz_exercise_page.dart index b9a7a03..fb82fef 100644 --- a/app/lib/features/quiz_exercise/presentation/pages/quiz_exercise_page.dart +++ b/app/lib/features/quiz_exercise/presentation/pages/quiz_exercise_page.dart @@ -163,75 +163,110 @@ class _QuizExercisePageState extends State { return state is QuizExerciseShow; }, builder: (context, state) { if (state is QuizExerciseShow) { - return AlertDialog( - content: SingleChildScrollView( - child: Column( - children: [ - Container( - alignment: Alignment.centerLeft, - padding: const EdgeInsets.symmetric(horizontal: 8), - child: Text( - 'Pertanyaan', - style: FontTheme.blackTextBold(), + return Scaffold( + backgroundColor: Colors.transparent, + body: AlertDialog( + content: SingleChildScrollView( + child: Column( + children: [ + Container( + alignment: Alignment.centerLeft, + padding: const EdgeInsets.symmetric(horizontal: 8), + child: Text( + 'Pertanyaan', + style: FontTheme.blackTextBold(), + ), ), - ), - SizedBox( - width: 400, - height: 200, - child: SingleChildScrollView( - child: - Html(data: state.quizExercise.question.content), + SizedBox( + width: 400, + height: 200, + child: SingleChildScrollView( + child: + Html(data: state.quizExercise.question.content), + ), ), - ), - ...state.quizExercise.question.options! - .map((e) => RadioListTile( - title: Text(e.content), - value: e.id, - groupValue: state.selectedAnswer, - onChanged: (value) { - context - .read() - .selectAnswer(e.id); - })), - state.quizExercise.type == 'SHORT_ANSWER' - ? Container( - padding: const EdgeInsets.only(top: 20), - child: CustomTextField('Jawaban anda', (value) { - context - .read() - .fillAnswer(value); - }, (p0) => null, ''), - ) - : Container(), - Text(state.modalErrorMessage), - ], - ), - ), - actions: [ - SizedBox( - width: 100, - height: 50, - child: Button( - buttonType: ButtonType.secondary, - text: 'Cancel', - onTap: () { - Navigator.pop(context); - }, + ...state.quizExercise.question.options! + .map((e) => RadioListTile( + title: Text(e.content), + value: e.id, + groupValue: state.selectedAnswer, + onChanged: (value) { + context + .read() + .selectAnswer(e.id); + })), + state.quizExercise.type == 'SHORT_ANSWER' + ? Container( + padding: const EdgeInsets.only(top: 20), + child: CustomTextField('Jawaban anda', (value) { + context + .read() + .fillAnswer(value); + }, (p0) => null, ''), + ) + : Container(), + Text(state.modalErrorMessage), + ], ), ), - SizedBox( - width: 100, - height: 50, - child: Button( - buttonType: ButtonType.tertiary, - text: 'OK', - onTap: () { - context.read().submitAnswer(); - Navigator.pop(context); - }, + actions: [ + SizedBox( + width: 100, + height: 50, + child: Button( + buttonType: ButtonType.secondary, + text: 'Cancel', + onTap: () { + Navigator.pop(context); + }, + ), ), - ), - ], + SizedBox( + width: 100, + height: 50, + child: Button( + buttonType: ButtonType.tertiary, + text: 'OK', + onTap: () { + var error = ''; + + if (state.quizExercise.type == 'SHORT_ANSWER') { + if (state.shortAnswer == '') { + error = 'Isi jawaban anda'; + } + } else { + if (state.selectedAnswer == '') { + error = 'Pilih salah satu jawaban'; + } + } + + if (error != '') { + final snackBar = SnackBar( + backgroundColor: Colors.red, + duration: const Duration(seconds: 1), + behavior: SnackBarBehavior.floating, + margin: EdgeInsets.only( + bottom: + MediaQuery.of(context).size.height - 150, + left: 10, + right: 10), + content: Text(error), + ); + + ScaffoldMessenger.of(context) + .showSnackBar(snackBar); + + error = ''; + return; + } + + context.read().submitAnswer(); + Navigator.pop(context); + }, + ), + ), + ], + ), ); } return const SizedBox(