diff --git a/aptiche/lib/views/quiz/quiz_controller.dart b/aptiche/lib/views/quiz/quiz_controller.dart index 02fc17e..769fec7 100644 --- a/aptiche/lib/views/quiz/quiz_controller.dart +++ b/aptiche/lib/views/quiz/quiz_controller.dart @@ -35,6 +35,8 @@ class QuizController extends GetxController { ///Stores the user score of the quiz Rx userScore = 0.obs; + ///[questionIndex] stream subscription + StreamSubscription? _questionIndexStream; // Functions /// A void function which controls the behaviour of the timer(`timeout`) @@ -61,15 +63,28 @@ class QuizController extends GetxController { }); } + @override + void onInit() { + _questionIndexStream = questionIndex.listen((_) { + update(); + }); + super.onInit(); + } + @override void dispose() { + _questionIndexStream?.cancel(); timer.cancel(); super.dispose(); } /// Changes [radioGroupValue] to the selected option. - void selectOption(ChoicesEnum newValue) { - radioGroupValue.value = newValue; + void selectOption(ChoicesEnum? newValue) { + if (newValue == null) { + radioGroupValue.value = ChoicesEnum.NON; + } else + radioGroupValue.value = newValue; + update(); } /// Checks whether the question is answered or not diff --git a/aptiche/lib/views/quiz/quiz_view.dart b/aptiche/lib/views/quiz/quiz_view.dart index 557d34a..4a32180 100644 --- a/aptiche/lib/views/quiz/quiz_view.dart +++ b/aptiche/lib/views/quiz/quiz_view.dart @@ -77,24 +77,24 @@ class QuizView extends GetView { mainAxisAlignment: MainAxisAlignment.spaceBetween, crossAxisAlignment: CrossAxisAlignment.center, children: [ - /* if (controller.questionIndex.value > 0) + if (controller.questionIndex.value > 0) CustomButton( horizontalPadding: SizeConfig.safeBlockHorizontal! * 1.4, verticalPadding: SizeConfig.safeBlockVertical! * 0.27, text: 'Previous', onTap: () { - print(controller.radioGroupValue.value); - //controller.previous(); + // print(controller.radioGroupValue.value); + controller.previous(); }, - ), */ - CustomButton( - horizontalPadding: SizeConfig.safeBlockHorizontal! * 1.4, - verticalPadding: SizeConfig.safeBlockVertical! * 0.27, - text: 'Clear Choice', - onTap: () { - controller.clearRadioGroup(); - }, - ), + ), + // CustomButton( + // horizontalPadding: SizeConfig.safeBlockHorizontal! * 1.4, + // verticalPadding: SizeConfig.safeBlockVertical! * 0.27, + // text: 'Clear Choice', + // onTap: () { + // controller.clearRadioGroup(); + // }, + // ), if (controller.questionIndex.value + 1 < controller.questions.length) CustomButton( diff --git a/aptiche/lib/widgets/quiz/choices_list_view.dart b/aptiche/lib/widgets/quiz/choices_list_view.dart index 4d2647f..7d95af7 100644 --- a/aptiche/lib/widgets/quiz/choices_list_view.dart +++ b/aptiche/lib/widgets/quiz/choices_list_view.dart @@ -3,9 +3,10 @@ import 'package:aptiche/utils/ui_scaling.dart'; import 'package:aptiche/views/quiz/quiz_controller.dart'; import 'package:aptiche/widgets/quiz/custom_radio.dart'; import 'package:flutter/material.dart'; +import 'package:get/get.dart'; import 'package:get/get_state_manager/get_state_manager.dart'; -class ChoicesListView extends GetView { +class ChoicesListView extends StatelessWidget { const ChoicesListView({ Key? key, }) : super(key: key); @@ -17,18 +18,21 @@ class ChoicesListView extends GetView { 4, (int index) => Column( children: [ - Obx( - () => CustomRadio( - label: controller - .questions[controller.questionIndex.value].options[index], - padding: EdgeInsets.zero, - groupValue: controller.radioGroupValue.value, - value: ChoicesEnum.values[index], - onChanged: (ChoicesEnum newValue) { - controller.selectOption(newValue); - }, - ), - ), + GetBuilder( + init: Get.find(), + builder: (QuizController controller) { + return CustomRadio( + label: controller.questions[controller.questionIndex.value] + .options[index], + padding: EdgeInsets.zero, + groupValue: controller.radioGroupValue.value, + value: ChoicesEnum.values[index], + onChanged: (ChoicesEnum? newValue) { + controller.selectOption(newValue); + }, + toggleable: true, + ); + }), Padding( padding: EdgeInsets.symmetric( horizontal: SizeConfig.safeBlockHorizontal! * 6, diff --git a/aptiche/lib/widgets/quiz/custom_radio.dart b/aptiche/lib/widgets/quiz/custom_radio.dart index 321768b..ccc2fde 100644 --- a/aptiche/lib/widgets/quiz/custom_radio.dart +++ b/aptiche/lib/widgets/quiz/custom_radio.dart @@ -9,8 +9,9 @@ class CustomRadio extends StatelessWidget { required this.groupValue, required this.value, required this.onChanged, + this.toggleable = false, }) : super(key: key); - + final bool toggleable; final String label; final EdgeInsets padding; final ChoicesEnum groupValue; @@ -23,7 +24,8 @@ class CustomRadio extends StatelessWidget { onTap: () { if (value != groupValue) { onChanged(value); - } + } else + onChanged(null); }, child: Padding( padding: padding, @@ -35,6 +37,7 @@ class CustomRadio extends StatelessWidget { Theme.of(context).primaryColor), groupValue: groupValue, value: value, + toggleable: toggleable, onChanged: (ChoicesEnum? newValue) { onChanged(newValue); },