diff --git a/lib/component/notification/delete_confirmation.dart b/lib/component/notification/delete_confirmation.dart new file mode 100644 index 0000000..2cad6c2 --- /dev/null +++ b/lib/component/notification/delete_confirmation.dart @@ -0,0 +1,69 @@ +import 'package:flutter/material.dart'; +import 'package:quiz_app/app/const/colors/app_colors.dart'; + +class DeleteQuestionDialog { + static Future show({ + required BuildContext context, + required VoidCallback onDelete, + }) async { + showDialog( + context: context, + barrierDismissible: true, + builder: (context) { + return Dialog( + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)), + child: Padding( + padding: const EdgeInsets.all(20), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + const Text( + "Hapus Soal?", + style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18, color: AppColors.darkText), + ), + const SizedBox(height: 16), + const Text( + "Soal ini akan dihapus dari daftar kuis. Yakin ingin menghapus?", + style: TextStyle(fontSize: 14, color: AppColors.softGrayText), + textAlign: TextAlign.center, + ), + const SizedBox(height: 24), + Row( + children: [ + Expanded( + child: ElevatedButton( + onPressed: () => Navigator.pop(context), + style: ElevatedButton.styleFrom( + backgroundColor: Colors.white, + foregroundColor: AppColors.primaryBlue, + side: const BorderSide(color: AppColors.primaryBlue), + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + ), + child: const Text("Batal"), + ), + ), + const SizedBox(width: 12), + Expanded( + child: ElevatedButton( + onPressed: () { + Navigator.pop(context); + onDelete(); + }, + style: ElevatedButton.styleFrom( + backgroundColor: AppColors.primaryBlue, + foregroundColor: Colors.white, + shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), + ), + child: const Text("Hapus"), + ), + ), + ], + ), + ], + ), + ), + ); + }, + ); + } +} diff --git a/lib/data/models/quiz/quiestion_data_model.dart b/lib/data/models/quiz/quiestion_data_model.dart index 9e37bd7..10a7880 100644 --- a/lib/data/models/quiz/quiestion_data_model.dart +++ b/lib/data/models/quiz/quiestion_data_model.dart @@ -25,6 +25,7 @@ class QuestionData { }); QuestionData copyWith({ + int? index, String? question, String? answer, List? options, @@ -32,7 +33,7 @@ class QuestionData { QuestionType? type, }) { return QuestionData( - index: index, + index: index ?? this.index, question: question ?? this.question, answer: answer ?? this.answer, options: options ?? this.options, diff --git a/lib/feature/quiz_creation/controller/quiz_creation_controller.dart b/lib/feature/quiz_creation/controller/quiz_creation_controller.dart index 041912a..dc5462a 100644 --- a/lib/feature/quiz_creation/controller/quiz_creation_controller.dart +++ b/lib/feature/quiz_creation/controller/quiz_creation_controller.dart @@ -3,6 +3,7 @@ import 'package:flutter/widgets.dart'; import 'package:get/get.dart'; import 'package:quiz_app/app/const/enums/question_type.dart'; import 'package:quiz_app/app/routes/app_pages.dart'; +import 'package:quiz_app/component/notification/delete_confirmation.dart'; import 'package:quiz_app/component/notification/pop_up_confirmation.dart'; import 'package:quiz_app/data/models/quiz/quiestion_data_model.dart'; @@ -142,4 +143,28 @@ class QuizCreationController extends GetxController { AppDialog.showExitConfirmationDialog(context); } } + + void showDeleteQuestionDialog(BuildContext context, int index) { + DeleteQuestionDialog.show(context: context, onDelete: () => onQuestionDelete(index)); + } + + void onQuestionDelete(int index) { + if (quizData.length <= 1) { + Get.snackbar('Tidak Bisa Dihapus', 'Minimal harus ada satu soal dalam kuis.'); + return; + } + + quizData.removeAt(index); + + for (int i = 0; i < quizData.length; i++) { + quizData[i] = quizData[i].copyWith(index: i + 1); + } + + if (selectedQuizIndex.value == index) { + selectedQuizIndex.value = 0; + onSelectedQuizItem(0); + } else if (selectedQuizIndex.value > index) { + selectedQuizIndex.value -= 1; + } + } } diff --git a/lib/feature/quiz_creation/view/component/custom_question_component.dart b/lib/feature/quiz_creation/view/component/custom_question_component.dart index 2e083d9..18b0ee2 100644 --- a/lib/feature/quiz_creation/view/component/custom_question_component.dart +++ b/lib/feature/quiz_creation/view/component/custom_question_component.dart @@ -15,6 +15,15 @@ class CustomQuestionComponent extends GetView { return Column( children: [ _buildNumberPicker(), + const SizedBox(height: 8), + const Text( + "*Tekan dan tahan soal untuk menghapus", + style: TextStyle( + fontSize: 12, + color: AppColors.softGrayText, + fontStyle: FontStyle.italic, + ), + ), const SizedBox(height: 20), _buildQuizTypeSelector(), const SizedBox(height: 20), @@ -65,6 +74,11 @@ class CustomQuestionComponent extends GetView { controller.onSelectedQuizItem(index); } }, + onLongPress: () { + if (!isLast) { + controller.showDeleteQuestionDialog(context, index); + } + }, child: Obx(() { bool isSelected = controller.selectedQuizIndex.value == index; return Container(