Home > Software engineering >  Index value after selecting an option is not updated while trying to use it in next question(Flutter
Index value after selecting an option is not updated while trying to use it in next question(Flutter

Time:11-02

Working with the logic to navigate next questions with NoOfRoutes condition. Getting the index in nextQuestionCopy under QuestionControllerCopy is the problem akka. Only 0th index is what we are getting. If we are able to get the proper index the navigation will be successful.

QuestionController Class :-

import 'dart:core';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:quiz_app/database/quiz_database.dart';
import 'package:quiz_app/models/Album.dart';
import 'package:quiz_app/models/Questions.dart';
import 'package:quiz_app/screens/score/score_screen.dart';

class QuestionControllerCopy extends GetxController
    with SingleGetTickerProviderMixin {

  static List savedList = [];
  BlazorQuiz note;
  AnimationController _animationController;
  Animation _animation;

  Animation get animation => this._animation;
  PageController _pageController;
  PageController get pageController => this._pageController;
  bool _isAnswered = false;
  bool get isAnswered => this._isAnswered;
  int _correctAns;
  int get correctAns => this._correctAns;
  int _selectedAns;
  int get selectedAns => this._selectedAns;
  RxInt _questionNumber = 1.obs;
  RxInt get questionNumber => this._questionNumber;
  int _numOfCorrectAns = 0;
  int get numOfCorrectAns => this._numOfCorrectAns;

  @override
  void initState(){
    _pageController = PageController();
    super.onInit();
  }

  @override
  Future<void> onInit() async {
    _animationController =
        AnimationController(duration: Duration(seconds: 6000),vsync: this);
    _animation = Tween<double>(begin: 0, end: 1).animate(_animationController)
      ..addListener(() {
        update();
      });
    _animationController.forward().whenComplete(() => nextQuestionCopy(note,0));

    _pageController = PageController();
    super.onInit();
  }

  @override
  void onClose() {
    super.onClose();
    _animationController.dispose();
    _pageController.dispose();
  }

  void checkAnsCopy(Quiz question, int selectedIndex) {
    _isAnswered = true;
    _correctAns = int.parse(question.answer_index);
    _selectedAns = selectedIndex;
    if (_correctAns == _selectedAns) _numOfCorrectAns  ;
    debugPrint("_correctAnswer : $_correctAns , _selectedAns : $_selectedAns");
    debugPrint("_numOfCorrectAns : $_numOfCorrectAns");
    _animationController.stop();
    update();
  }

  void checkAns(BlazorQuiz question, int selectedIndex) {
    _isAnswered = true;
    _selectedAns = selectedIndex;
    debugPrint("_correctAnswer : $_correctAns , _selectedAns : $_selectedAns");
    _animationController.stop();
    update();
  }

  final qdb = QuizDatabase.instance;

  Future<void>selectedOptions(int index) async {
    debugPrint('index check radio outside $index');
    if(note.NoOfRoutes == 4){
      debugPrint('index check radio inside $index');
      if(index == 0){
        await _pageController.jumpToPage(note.Route1-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }
      if(index == 1){
        await _pageController.jumpToPage(note.Route2-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }
      if(index == 2){
        await _pageController.jumpToPage(note.Route3-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }
      if(index == 3){
        await _pageController.jumpToPage(note.Route4-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }
    }

    return index;
  }
  Future<void> selectedOptionsCheck(int index) async {

    debugPrint('index check checkIndex $index');
    nextQuestionCopy(note,index);
    if(note.NoOfRoutes == 4){

      debugPrint('index check checkIndex inside $index');
      if(index == 0){
        _pageController.jumpToPage(note.Route1-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }
      if(index == 1){
        _pageController.jumpToPage(note.Route2-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }
      if(index == 2){
        _pageController.jumpToPage(note.Route3-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }
      if(index == 3){
        _pageController.jumpToPage(note.Route4-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }
    }

    return index;
  }
  Future<void> nextQuestionCopy(BlazorQuiz note,int index) async {
    await qdb.updateBlazor(note);
    qdb.updateBlazorAnswer(note);

      debugPrint('Entered nextQuestion for Update Blazor : $note, index $index');
      debugPrint("NoOfRoutes==1$index");

      if(note.NoOfRoutes == 1){
        await _pageController.jumpToPage(note.Route1-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }

      if(note.NoOfRoutes == 4){
          debugPrint('index check checkIndex inside $index');
        if(index == 0){
          await _pageController.jumpToPage(note.Route1-1);
          _animationController.reset();
          _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
        }
        if(index == 1){
          await _pageController.jumpToPage(note.Route2-1);
          _animationController.reset();
          _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
        }
        if(index == 2){
          await _pageController.jumpToPage(note.Route3-1);
          _animationController.reset();
          _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
        }
        if(index == 3){
          await _pageController.jumpToPage(note.Route4-1);
          _animationController.reset();
          _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
        }
      }

    if(note.NoOfRoutes == 2){

      debugPrint('index check checkIndex inside $index');
      if(index == 0){
        await _pageController.jumpToPage(note.Route1-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }
      if(index == 1){
        await _pageController.jumpToPage(note.Route2-1);
        _animationController.reset();
        _animationController.forward().whenComplete(() => nextQuestionCopy(note,index));
      }
    }
  }

  scoreNavigation(int index) {
    Get.to(ScoreScreen(index: index));
  }

  updateTheQnNum(int index) {
    _questionNumber.value = index   1;
    return _questionNumber.value;
  }
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Question card widget Radio Class :-

import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:get/get_instance/src/extension_instance.dart';
import 'package:get/get_state_manager/src/rx_flutter/rx_obx_widget.dart';
import 'package:quiz_app/controllers/question_controller_copy.dart';
import 'package:quiz_app/models/Album.dart';
import 'package:quiz_app/models/Questions.dart';
import 'package:quiz_app/screens/quiz/components/option.dart';
import 'package:quiz_app/screens/quiz/components/option_copy.dart';
import 'package:quiz_app/screens/quiz/components/option_radio_button.dart';
import 'package:quiz_app/screens/quiz/components/progress_bar.dart';
import 'package:quiz_app/screens/quiz/components/question_card.dart';

import '../../../constants.dart';

class QuestionCardWidgetRadio extends StatelessWidget {
  OptionRadio optionRadio =OptionRadio();
  QuestionCardWidgetRadio({
    Key key,
    this.note,
    this.index,
    this.questionLength,
  }) : super(key: key);

  final BlazorQuiz note;
  final int index;
  final int questionLength;
  bool _isAnswered = false;
  bool get isAnswered => this._isAnswered;


  @override
  Widget build(BuildContext context) {

    final qnID = "Question No : "   note.Id.toString();

    QuestionControllerCopy _questionController =
        Get.put(QuestionControllerCopy());
    debugPrint("Answer Print : ${note.Answer.split(",")}");

    return Container(

        margin: EdgeInsets.symmetric(horizontal: kDefaultPadding, vertical: 35),
        decoration: BoxDecoration(
          color: Colors.white,
        borderRadius: BorderRadius.circular(25),
        ),
        padding: EdgeInsets.all(kDefaultPadding),
    child: SingleChildScrollView(
    scrollDirection: Axis.vertical,
        child: Column(
          children: [
            Text(
              qnID,
              style: TextStyle(
                color: Colors.black,
                fontSize: 18,
                fontWeight: FontWeight.normal,
              ),
            ),
            Text(
              note.Question,
              style: TextStyle(
                color: Colors.black,
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ),
            ),
            SizedBox(height: kDefaultPadding / 2),

            ...List.generate(
              note.Answer.split(",").length,
              (index) => OptionRadio(
                index: index,
                text: note.Answer.split(",")[index],
                press: () =>{
                  _isAnswered = true,
                  _questionController.checkAns(note, index),
                }
              ),

            ),

            TextButton(
              onPressed: (){
                debugPrint('index value updated$index');
                debugPrint("Clicked options : ${note.Answer.isNotEmpty} isAnswered: $isAnswered");
                debugPrint('Check Index : ${index} check quiz model Lenght : ${QuizModel.question.length} check note Id : ${note.Id}');
                if(index 1 == questionLength){
                  // score screen
                  debugPrint('Navigate to score screen');
                  Text(
                    "Submit",
                    style: Theme.of(context)
                        .textTheme
                        .button
                        .copyWith(color: Colors.white),
                  );
                  _questionController.scoreNavigation(index);
                }
                else{
                  if (note.Answer.isNotEmpty == !isAnswered) {
                    debugPrint('Clickkkkkked${optionRadio.index}');
                    _questionController.nextQuestionCopy(note,index);
                  }else{
                    debugPrint('Clickkkkk proper option');
                  }
                }
              },

              child: Container(

                width: double.infinity,
                alignment: Alignment.center,
                padding: EdgeInsets.all(kDefaultPadding * 0.75),
                margin: EdgeInsets.all(37),
                // 15
                decoration: BoxDecoration(
                  gradient: kT2Gradient2,
                  borderRadius: BorderRadius.all(Radius.circular(0)),
                ),
                child: Text(
                  "Next Question",
                  style: Theme.of(context)
                      .textTheme
                      .button
                      .copyWith(color: Colors.white),
                ),
              ),
            ),

          ],
        ),
      ),
    );
  }

}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Option Radio Class :-

import 'package:flutter/material.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:quiz_app/controllers/question_controller_copy.dart';
import '../../../constants.dart';
import 'package:flutter/src/material/radio_list_tile.dart';

class OptionRadio extends StatefulWidget {
  final String text;
  final int index;
  final VoidCallback press;

  const OptionRadio({
    Key key,
    this.text,
    this.index,
    this.press,
  }) : super();

  @override
  OptionRadioPage createState() =>
      OptionRadioPage(this.text, this.index, this.press);

}

class OptionRadioPage extends State<OptionRadio> {
  final String text;
  int index;
  final VoidCallback press;
  QuestionControllerCopy controllerCopy =QuestionControllerCopy();

  int id = 1;
  int val = -1;

  int selectedButton = 0;
  bool _isButtonDisabled;

  OptionRadioPage(this.text, this.index, this.press);

  @override
  void initState() {
    _isButtonDisabled = false;
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<QuestionControllerCopy>(
        init: QuestionControllerCopy(),
        builder: (qnController) {
          Color getTheRightColor() {
            if (qnController.isAnswered) {
              if (index == qnController.selectedAns &&
                  qnController.selectedAns == qnController.correctAns) {
                return kBlackColor;
              } else if (index == qnController.selectedAns &&
                  qnController.selectedAns != qnController.correctAns) {
                return kBlackColor;
              }
            }
            return kBlackColor;
          }

          return InkWell(
            onTap: press,
            child: Container(
              child: Row(
                children: <Widget>[
                  Expanded(
                    child: Container(
                        height: 60.0,
                        child: Theme(
                          data: Theme.of(context).copyWith(
                              unselectedWidgetColor: Colors.grey,
                              disabledColor: Colors.blue),
                          child: Column(children: [
                       
                            RadioListTile(
                              title: Text(
                                "${index   1}. $text",
                                style: TextStyle(
                                    color: getTheRightColor(), fontSize: 16),
                                softWrap: true,
                              ),
                              groupValue: selectedButton,
                              value: index,
                              activeColor: Colors.green,
                              onChanged: (val) {

                                debugPrint(
                                    'Radio button is clicked onChanged $val');
                                setState(() {
                                  debugPrint('Radio button setState $val');
                                  selectedButton = val;

                                  debugPrint(
                                      'Radio button is clicked onChanged $index');
                                  controllerCopy.selectedOptions(index);
                                  // var answerClicked = index;

                                });
                              },
                              toggleable: true,
                            ),
                          ]),
                        )),
                  ),
                ],
              ),
            ),
          );
        });

  }

  @override
  State<StatefulWidget> createState() {
    throw UnimplementedError();
  }

}
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

How to get the selected index value from OptionRadio Class to QuestionCardWidgetRadio Class properly

CodePudding user response:

Solved it myself. Use SharedPreferences to pass the selected index value to condition.

Set sharedpreferences key, value in OptionRadio.dart class,

 SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('intValue', index);
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Get sharedpreferences key, value in QuestionCardWidgetRadio.dart class,

SharedPreferences prefs = await SharedPreferences.getInstance();
int intValue = prefs.getInt('intValue');
_questionController.nextQuestionCopy(note,intValue);
prefs.remove("intValue");
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related