feat: adding index delete on the question maker

This commit is contained in:
akhdanre 2025-04-27 15:19:13 +07:00
parent eaf97e969f
commit 261d094d94
4 changed files with 110 additions and 1 deletions

View File

@ -0,0 +1,69 @@
import 'package:flutter/material.dart';
import 'package:quiz_app/app/const/colors/app_colors.dart';
class DeleteQuestionDialog {
static Future<void> 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"),
),
),
],
),
],
),
),
);
},
);
}
}

View File

@ -25,6 +25,7 @@ class QuestionData {
}); });
QuestionData copyWith({ QuestionData copyWith({
int? index,
String? question, String? question,
String? answer, String? answer,
List<OptionData>? options, List<OptionData>? options,
@ -32,7 +33,7 @@ class QuestionData {
QuestionType? type, QuestionType? type,
}) { }) {
return QuestionData( return QuestionData(
index: index, index: index ?? this.index,
question: question ?? this.question, question: question ?? this.question,
answer: answer ?? this.answer, answer: answer ?? this.answer,
options: options ?? this.options, options: options ?? this.options,

View File

@ -3,6 +3,7 @@ import 'package:flutter/widgets.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
import 'package:quiz_app/app/const/enums/question_type.dart'; import 'package:quiz_app/app/const/enums/question_type.dart';
import 'package:quiz_app/app/routes/app_pages.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/component/notification/pop_up_confirmation.dart';
import 'package:quiz_app/data/models/quiz/quiestion_data_model.dart'; import 'package:quiz_app/data/models/quiz/quiestion_data_model.dart';
@ -142,4 +143,28 @@ class QuizCreationController extends GetxController {
AppDialog.showExitConfirmationDialog(context); 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;
}
}
} }

View File

@ -15,6 +15,15 @@ class CustomQuestionComponent extends GetView<QuizCreationController> {
return Column( return Column(
children: [ children: [
_buildNumberPicker(), _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), const SizedBox(height: 20),
_buildQuizTypeSelector(), _buildQuizTypeSelector(),
const SizedBox(height: 20), const SizedBox(height: 20),
@ -65,6 +74,11 @@ class CustomQuestionComponent extends GetView<QuizCreationController> {
controller.onSelectedQuizItem(index); controller.onSelectedQuizItem(index);
} }
}, },
onLongPress: () {
if (!isLast) {
controller.showDeleteQuestionDialog(context, index);
}
},
child: Obx(() { child: Obx(() {
bool isSelected = controller.selectedQuizIndex.value == index; bool isSelected = controller.selectedQuizIndex.value == index;
return Container( return Container(