-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion_screen.dart
67 lines (61 loc) · 1.9 KB
/
question_screen.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import 'package:flutter/material.dart';
import 'package:quiz_app/answer_button.dart';
import 'package:quiz_app/data/questions.dart';
class QuestionScreen extends StatefulWidget {
const QuestionScreen({
super.key,
required this.onSelectAnswer,
});
final void Function(String answer) onSelectAnswer;
@override
State<QuestionScreen> createState() {
return _QuestionScreen();
}
}
class _QuestionScreen extends State<QuestionScreen> {
var currentQuestionIndex = 0;
void answerQuestion(String selectedAnswer) {
widget.onSelectAnswer(selectedAnswer);
// currentQuestionIndex = currentQuestionIndex + 1;
// currentQuestionIndex += 1; // Here the value can be set manually eg. variable += 2
setState(() {
currentQuestionIndex++; // increments the value by 1 and only by 1
});
}
@override
Widget build(context) {
final currentQuestion = questions[currentQuestionIndex];
return SizedBox(
width: double.infinity, // Using as much width as available
child: Container(
margin: const EdgeInsetsDirectional.all(40),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
currentQuestion.text,
style: TextStyle(
color: const Color.fromARGB(255, 201, 163, 249),
fontSize: 24,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 30),
...currentQuestion.getShuffleAnswers().map(
(answer) {
return AnswerButton(
answerText: answer,
onTap: () {
answerQuestion(answer);
},
);
},
),
],
),
),
);
}
}