选择一个选项后的索引值在下一个问题中尝试使用它时不会更新(Flutter-SQLite-Dart)

Index value after selecting an option is not updated while trying to use it in next question(Flutter-SQLite-Dart)

使用 NoOfRoutes 条件导航下一个问题的逻辑。在QuestionControllerCopy下获取nextQuestionCopy中的索引是问题akka。我们得到的只有第 0 个索引。如果我们能够获得正确的索引,导航就会成功。

问题控制器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;
  }
}

问题卡小部件单选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),
                ),
              ),
            ),

          ],
        ),
      ),
    );
  }

}

选项收音机 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();
  }

}

如何从 OptionRadio Class 正确获取 QuestionCardWidgetRadio Class 中选择的索引值

自己解决了。使用 SharedPreferences 将选定的索引值传递给条件。

设置 sharedpreferences 键,值在 OptionRadio.dart class,

 SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('intValue', index);

获取 sharedpreferences 键,值在 QuestionCardWidgetRadio.dart class,

SharedPreferences prefs = await SharedPreferences.getInstance();
int intValue = prefs.getInt('intValue');
_questionController.nextQuestionCopy(note,intValue);
prefs.remove("intValue");