I made a quiz application that moves to the next page when a button is pressed. However, I would like to delete the button and replace it with a function that automatically advances to the next page after being scored when you manually enter an answer. How can I change the press: () code to do this?
class Option extends StatelessWidget {
const Option({
Key? key,
required this.text,
required this.index,
required this.press,
}) : super(key: key);
final String text;
final int index;
final VoidCallback press;
class QuestionCard extends StatelessWidget {
const QuestionCard({
Key ?key,
required this.question,
}) : super(key: key);
final Question question;
@override
Widget build(BuildContext context) {
QuestionController _controller = Get.put(QuestionController());
return Container(
margin: EdgeInsets.symmetric(horizontal: 10),
child: Column(
children: [
SizedBox(height: 10),
...List.generate(
question.options.length,
(index) => Option(
index: index,
text: question.options[index],
press: () => _controller.checkAns(question, index),
),
),
],
),
);
}
}
void checkAns(Question question, String selectedIndex) {
_isAnswered = true;
_correctAns = question.options;
_selectedAns = selectedIndex;
if (_correctAns == _selectedAns) _numOfCorrectAns ;
update();
Future.delayed(Duration(seconds: 1), () {
if (_questionNumber.value != _questions.length) {
_isAnswered = false;
_pageController.nextPage(
duration: Duration(milliseconds: 100), curve: Curves.ease);
} else {
Get.off(ScoreScreen());
}
});
}
CodePudding user response:
I think we should - first - deepen the logic behind this choice: when exactly do you expect the app to advance to the next page / question?
Say your user just finished typing, how can you recognize that he finished to answer? Maybe he just paused to, say, think a little more.
I'd personally choose to add a textInputAction: TextInputAction.done
onto the last form in the current page. Although this wouldn't work on Web or Desktop.
CodePudding user response:
Try to wrap it with a gesture detector (https://api.flutter.dev/flutter/widgets/GestureDetector-class.html), or you can use navigator.push to a new screen (https://flutter.dev/docs/cookbook/navigation/navigation-basics).