Trying to build an Exam app where I'm getting only one question at a time on each hit from API. What I'm trying to do is to build question and start the timer (90-0) when question is displayed. After timer is 0 or submit button is pressed, the new question is build (after getting data from API) and timer starts again from 90 seconds. At the moment, timer goes from 90 to 0 and stops and doesn't start after setState
I have seen and tried other solutions like creating a startTimer()
function but TweenAnimationBuilder
seems fit for the task. However, couldn't figure out to start it again using onEnd
.
Here is my TweenAnimationBuilder:
TweenAnimationBuilder(
tween: Tween(begin: 90.0, end: 0.0),
duration: Duration(seconds: 90),
builder: (context, value, child) {
num val = value;
num time = val.toInt();
return Stack(
alignment: Alignment.center,
children: [
normalText(
text: "$time",
size: 25,
color: color__orange),
SizedBox(
width: 60,
height: 60,
child: CircularProgressIndicator(
value: time /
90,
valueColor: const AlwaysStoppedAnimation(
color__orange),
),
),
]
);
},
onEnd: (){
var questionID = snapshot.data!.quizzesData!.first.questionID;
QuizQuestion(questionID);
setState(() {
});
},
),
And Here is the Submit Button:
ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: color__orange ),
onPressed: (){
if (selectedOptionStatus == 'True') {
var questionID = snapshot.data!.quizzesData!.first.questionID;
var score = snapshot.data!.quizzesData!.first.question!.first.marks;
var answerID = snapshot.data!.quizzesData!.first.question!.first.answer![index].answerID;
QuizQuestion(questionID);
StudentQuestionAnswer(questionID, score, answerID);
// currentQuestionIndex = int.parse(widget.totalQuestions) - remainingQuestions;
currentQuestionIndex++;
if ( (currentQuestionIndex + 1) == int.parse(widget.totalQuestions)) {
updateStudentQuizLoginHistoryFinish();
}
setState(() {});
}
else {
if (selectedOptionStatus == 'False') {
var questionID = snapshot.data!.quizzesData!.first.questionID;
var score = '0';
var answerID = snapshot.data!.quizzesData!.first.question!.first.answer![index].answerID;
QuizQuestion(questionID);
StudentQuestionAnswer(questionID, score, answerID);
currentQuestionIndex++;
if (currentQuestionIndex + 1 == int.parse(widget.totalQuestions)) {
StudentQuiz();
}
setState(() {});
}
}
},
child: const Text('Save Answer')
)
Provide key to TweenAnimationBuilder
key: ValueKey(questionId)
This will force the previous TweenAnimationBuilder to be disposed and new one will be created and attached.