Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

179 revamp v2 halaman detail soal #184

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/lib/features/quiz_exercise/presentation/pages/v2/_pages.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

import '../../../../../core/bases/widgets/layout/bebras_scaffold.dart';
import '../../../../quiz_download/presentation/bloc/quiz_registration_cubit.dart';
import '../../../../quiz_start/presentation/bloc/quiz_start_cubit.dart';
import '../../bloc/quiz_exercise_cubit.dart';

import 'task_dialog.dart';
import 'task_view.dart';

part 'quiz_exercise_page.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
part of '_pages.dart';

class QuizExercisePageV2 extends StatefulWidget {
final String? quizParticipantId;
const QuizExercisePageV2({super.key, this.quizParticipantId});

@override
State<QuizExercisePageV2> createState() => _QuizExercisePageV2State();
}

class _QuizExercisePageV2State extends State<QuizExercisePageV2> {
@override
void initState() {
final cubit = context.read<QuizExerciseCubit>();
if (cubit.quizParticipantId == widget.quizParticipantId &&
cubit.state is QuizExercisePaused) {
cubit.resume();
} else {
cubit.initialize(quizParticipantId: widget.quizParticipantId);
}
super.initState();
}

@override
void dispose() {
context.read<QuizExerciseCubit>().pause();
super.dispose();
}

@override
Widget build(BuildContext context) {
return BebrasScaffold(
avoidBottomInset: false,
body: SingleChildScrollView(
child: Stack(
children: [
Padding(
padding: const EdgeInsets.only(
left: 15,
top: 20,
right: 16,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: const Icon(Icons.arrow_back),
),
const Flexible(
child: Center(
child: Text(
'Bebras Pandai',
textAlign: TextAlign.center,
style:
TextStyle(fontWeight: FontWeight.bold, fontSize: 16)
),
),
),
GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: const Icon(Icons.list),
),
],
),
const SizedBox(
height: 10,
),
BlocConsumer<QuizExerciseCubit, QuizExerciseState>(
listenWhen: (context, state) {
return state is QuizExerciseFinished;
}, listener: (context, state) {
if (state is QuizExerciseFinished) {
context.pushReplacement(
Uri(
path: '/quiz_result',
queryParameters: {
'quiz_participant_id': state.quizParticipantId,
},
).toString(),
);
context
.read<QuizStartCubit>()
.initialize(widget.quizParticipantId);
context
.read<QuizRegistrationCubit>()
.fetchParticipantWeeklyQuiz();
}
},
buildWhen: (context, state) {
return state is! QuizExerciseFinished;
},
builder: (context, state) {
if (state is QuizExerciseLoading) {
return const Center(
child: CircularProgressIndicator(),
);
}
if (state is QuizExerciseFailed) {
return Text(state.error);
}
if (state is QuizExerciseShow) {
return TaskView(
task: state.quizExercise,
context: context,
remainingDuration: state.remainingDuration,
attempt: state.attempt,
onTaskTap: () {
onAnswerTap();
},
showPreviousButton: state.currentProblemIndex > 0,
showNextButton: state.currentProblemIndex <
(state.totalProblem - 1),
);
}
return Container();
}
),
],
),
),
],
),
),
);
}

Future<void> onAnswerTap() {
return showModalBottomSheet<void>(
context: context,
builder: (BuildContext context) {
return BlocBuilder<QuizExerciseCubit, QuizExerciseState>(
buildWhen: (context, state) {
return state is QuizExerciseShow;
},
builder: (context, state) {
if (state is QuizExerciseShow) {
return StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return TaskDialog(
task: state.quizExercise,
preview: false,
answer: state.answer.answer,
error: state.modalErrorMessage,
);
// return Container(
// height: 200,
// width: double.infinity,
// decoration: const BoxDecoration(
// color: Colors.white,
// borderRadius: BorderRadius.only(
// topLeft: Radius.circular(42),
// topRight: Radius.circular(42),
// ),
// ),
// child: TaskDialog(
// task: state.quizExercise,
// preview: false,
// answer: state.answer.answer,
// error: state.modalErrorMessage,
// ),
// );
}
);
} else {
return const SizedBox(
width: 100,
height: 100,
);
}
},
);
},
);

}

// void onAnswerTap() {
// showDialog(
// context: context,
// builder: (context) {
// return BlocBuilder<QuizExerciseCubit, QuizExerciseState>(
// buildWhen: (context, state) {
// return state is QuizExerciseShow;
// },
// builder: (context, state) {
// if (state is QuizExerciseShow) {
// return TaskDialog(
// task: state.quizExercise,
// preview: false,
// answer: state.answer.answer,
// error: state.modalErrorMessage,
// );
// }
// return const SizedBox(
// width: 100,
// height: 100,
// );
// });
// }
// );
// }
}
132 changes: 132 additions & 0 deletions app/lib/features/quiz_exercise/presentation/pages/v2/task_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../../core/bases/widgets/atoms/button.dart';
import '../../../../../core/bases/widgets/atoms/html_cached_image.dart';
import '../../../../../core/constants/assets.dart';
import '../../../../../core/theme/base_colors.dart';
import '../../../../authentication/register/presentation/widgets/custom_text_field.dart';
import '../../bloc/quiz_exercise_cubit.dart';
import '../../model/quiz_exercise.dart';

class TaskDialog extends StatelessWidget {
final String? answer;
final String? error;
final bool preview;
final QuizExercise task;
const TaskDialog({
required this.task,
required this.preview,
super.key,
this.answer,
this.error,
});

@override
Widget build(BuildContext context) {
return Container(
constraints: const BoxConstraints(minHeight: 30),
width: double.infinity,
color: Colors.white,
child: SizedBox(
height: MediaQuery.of(context).size.height,
child: Column(
children: [
if (task.type == 'MULTIPLE_CHOICE')
...task.question.options.asMap().entries.map((e) {
final current = String.fromCharCode(65 + e.key);

return RadioListTile(
title: SizedBox(
child: Transform.translate(
offset: const Offset(-20, 0),
child: Row(
children: [
Text('$current. '),
task.type == 'MULTIPLE_CHOICE_IMAGE'
? Image.network(
e.value.content.replaceAll(
Assets.sourceImg, Assets.urlImg),
width: MediaQuery.of(context).size.width - 240,
)
: Flexible(
child: HtmlWithCachedImages(data: e.value.content),
)
],
),
),
),
value: e.value.id,
groupValue: answer,
onChanged: (value) {
context.read<QuizExerciseCubit>().selectAnswer(e.value.id);
},
);
}),
if (task.type == 'SHORT_ANSWER')
Container(
padding: const EdgeInsets.only(top: 20),
child: CustomTextField(
'Jawaban anda',
(value) {
context.read<QuizExerciseCubit>().fillAnswer(value);
},
(p0) => null,
answer,
),
),
if (!preview) if (error != null) Text(error!),
Padding(
padding: const EdgeInsets.only(
left: 25,
right: 25,
),
child: Expanded(
child: Button(
innerHorizontalPadding: 4,
innerVerticalPadding: 8,
fontSize: 13,
text: 'Pilih Jawaban',
customButtonColor: BaseColors.brightBlue,
customTextColor: Colors.white,
borderRadius: 4,
onTap: () {
var error = '';
if (task.type == 'SHORT_ANSWER') {
if (answer == '') {
error = 'Isi jawaban anda';
}
} else {
if (answer == '') {
error = 'Pilih salah satu jawaban';
}
}

if (error != '') {
final snackBar = SnackBar(
backgroundColor: Colors.red,
duration: const Duration(seconds: 1),
behavior: SnackBarBehavior.floating,
margin: const EdgeInsets.only(
bottom: 50, left: 10, right: 10),
content: Text(error),
);

ScaffoldMessenger.of(context).showSnackBar(snackBar);

error = '';
return;
}

context.read<QuizExerciseCubit>().submitAnswer();
Navigator.pop(context);
},
),
),
),
],
),
),
);
}
}
Loading
Loading