Значение индекса после выбора параметра не обновляется при попытке использовать его в следующем вопросе(Flutter-SQLite-Dart)

#flutter #dart #radio-button #mobile-application #radiobuttonlist

Вопрос:

Работа с логикой для перехода к следующим вопросам с условием отсутствия ошибок. Получение индекса в nextQuestionCopy в разделе QuestionControllerCopy-это проблема akka. Только 0-й индекс-это то, что мы получаем. Если мы сможем получить правильный индекс, навигация будет успешной.

Класс управления вопросами :-

 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 =gt; this._animation;  PageController _pageController;  PageController get pageController =gt; this._pageController;  bool _isAnswered = false;  bool get isAnswered =gt; this._isAnswered;  int _correctAns;  int get correctAns =gt; this._correctAns;  int _selectedAns;  int get selectedAns =gt; this._selectedAns;  RxInt _questionNumber = 1.obs;  RxInt get questionNumber =gt; this._questionNumber;  int _numOfCorrectAns = 0;  int get numOfCorrectAns =gt; this._numOfCorrectAns;   @override  void initState(){  _pageController = PageController();  super.onInit();  }   @override  Futurelt;voidgt; onInit() async {  _animationController =  AnimationController(duration: Duration(seconds: 6000),vsync: this);  _animation = Tweenlt;doublegt;(begin: 0, end: 1).animate(_animationController)  ..addListener(() {  update();  });  _animationController.forward().whenComplete(() =gt; 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;   Futurelt;voidgt;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(() =gt; nextQuestionCopy(note,index));  }  if(index == 1){  await _pageController.jumpToPage(note.Route2-1);  _animationController.reset();  _animationController.forward().whenComplete(() =gt; nextQuestionCopy(note,index));  }  if(index == 2){  await _pageController.jumpToPage(note.Route3-1);  _animationController.reset();  _animationController.forward().whenComplete(() =gt; nextQuestionCopy(note,index));  }  if(index == 3){  await _pageController.jumpToPage(note.Route4-1);  _animationController.reset();  _animationController.forward().whenComplete(() =gt; nextQuestionCopy(note,index));  }  }   return index;  }  Futurelt;voidgt; 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(() =gt; nextQuestionCopy(note,index));  }  if(index == 1){  _pageController.jumpToPage(note.Route2-1);  _animationController.reset();  _animationController.forward().whenComplete(() =gt; nextQuestionCopy(note,index));  }  if(index == 2){  _pageController.jumpToPage(note.Route3-1);  _animationController.reset();  _animationController.forward().whenComplete(() =gt; nextQuestionCopy(note,index));  }  if(index == 3){  _pageController.jumpToPage(note.Route4-1);  _animationController.reset();  _animationController.forward().whenComplete(() =gt; nextQuestionCopy(note,index));  }  }   return index;  }  Futurelt;voidgt; 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(() =gt; 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(() =gt; nextQuestionCopy(note,index));  }  if(index == 1){  await _pageController.jumpToPage(note.Route2-1);  _animationController.reset();  _animationController.forward().whenComplete(() =gt; nextQuestionCopy(note,index));  }  if(index == 2){  await _pageController.jumpToPage(note.Route3-1);  _animationController.reset();  _animationController.forward().whenComplete(() =gt; nextQuestionCopy(note,index));  }  if(index == 3){  await _pageController.jumpToPage(note.Route4-1);  _animationController.reset();  _animationController.forward().whenComplete(() =gt; 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(() =gt; nextQuestionCopy(note,index));  }  if(index == 1){  await _pageController.jumpToPage(note.Route2-1);  _animationController.reset();  _animationController.forward().whenComplete(() =gt; nextQuestionCopy(note,index));  }  }  }   scoreNavigation(int index) {  Get.to(ScoreScreen(index: index));  }   updateTheQnNum(int index) {  _questionNumber.value = index   1;  return _questionNumber.value;  } } 

Класс Радио виджета карточки с вопросами :-

 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 =gt; 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) =gt; OptionRadio(  index: index,  text: note.Answer.split(",")[index],  press: () =gt;{  _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),  ),  ),  ),   ],  ),  ),  );  }  } 

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() =gt;  OptionRadioPage(this.text, this.index, this.press);  }  class OptionRadioPage extends Statelt;OptionRadiogt; {  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 GetBuilderlt;QuestionControllerCopygt;(  init: QuestionControllerCopy(),  builder: (qnController) {  Color getTheRightColor() {  if (qnController.isAnswered) {  if (index == qnController.selectedAns amp;amp;  qnController.selectedAns == qnController.correctAns) {  return kBlackColor;  } else if (index == qnController.selectedAns amp;amp;  qnController.selectedAns != qnController.correctAns) {  return kBlackColor;  }  }  return kBlackColor;  }   return InkWell(  onTap: press,  child: Container(  child: Row(  children: lt;Widgetgt;[  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  Statelt;StatefulWidgetgt; createState() {  throw UnimplementedError();  }  } 

Как правильно получить выбранное значение индекса из класса OptionRadio в класс QuestionCardWidgetRadio

Ответ №1:

Решил ее сам. Используйте SharedPreferences для передачи выбранного значения индекса в условие.

Установите ключ sharedpreferences, значение в OptionRadio.класс дротиков,

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

Получите ключ sharedpreferences, значение в QuestionCardWidgetRadio.класс дротиков,

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