-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathtex_view_quiz_example.dart
214 lines (205 loc) · 6.59 KB
/
tex_view_quiz_example.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import 'package:flutter/material.dart';
import 'package:flutter_tex/flutter_tex.dart';
class Quiz {
final String statement;
final List<QuizOption> options;
final String correctOptionId;
Quiz(
{required this.statement,
required this.options,
required this.correctOptionId});
}
class QuizOption {
final String id;
final String option;
QuizOption(this.id, this.option);
}
class TeXViewQuizExample extends StatefulWidget {
const TeXViewQuizExample({super.key});
@override
State<TeXViewQuizExample> createState() => _TeXViewQuizExampleState();
}
class _TeXViewQuizExampleState extends State<TeXViewQuizExample> {
int currentQuizIndex = 0;
String selectedOptionId = "";
bool isWrong = false;
List<Quiz> quizList = [
Quiz(
statement: r"""<h3>What is the correct form of quadratic formula?</h3>""",
options: [
QuizOption(
"id_1",
r""" <h2>(A) \(x = {-b \pm \sqrt{b^2+4ac} \over 2a}\)</h2>""",
),
QuizOption(
"id_2",
r""" <h2>(B) \(x = {b \pm \sqrt{b^2-4ac} \over 2a}\)</h2>""",
),
QuizOption(
"id_3",
r""" <h2>(C) \(x = {-b \pm \sqrt{b^2-4ac} \over 2a}\)</h2>""",
),
QuizOption(
"id_4",
r""" <h2>(D) \(x = {-b + \sqrt{b^2+4ac} \over 2a}\)</h2>""",
),
],
correctOptionId: "id_3",
),
Quiz(
statement:
r"""<h3>Choose the correct mathematical form of Bohr's Radius.</h3>""",
options: [
QuizOption(
"id_1",
r""" <h2>(A) \( a_0 = \frac{{\hbar ^2 }}{{m_e ke^2 }} \)</h2>""",
),
QuizOption(
"id_2",
r""" <h2>(B) \( a_0 = \frac{{\hbar ^2 }}{{m_e ke^3 }} \)</h2>""",
),
QuizOption(
"id_3",
r""" <h2>(C) \( a_0 = \frac{{\hbar ^3 }}{{m_e ke^2 }} \)</h2>""",
),
QuizOption(
"id_4",
r""" <h2>(D) \( a_0 = \frac{{\hbar }}{{m_e ke^2 }} \)</h2>""",
),
],
correctOptionId: "id_1",
),
Quiz(
statement: r"""<h3>Select the correct Chemical Balanced Equation.</h3>""",
options: [
QuizOption(
"id_1",
r""" <h2>(A) \( \ce{CO + C -> 2 CO} \)</h2>""",
),
QuizOption(
"id_2",
r""" <h2>(B) \( \ce{CO2 + C -> CO} \)</h2>""",
),
QuizOption(
"id_3",
r""" <h2>(C) \( \ce{CO + C -> CO} \)</h2>""",
),
QuizOption(
"id_4",
r""" <h2>(D) \( \ce{CO2 + C -> 2 CO} \)</h2>""",
),
],
correctOptionId: "id_4",
),
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: const Text("TeXView Quiz"),
),
body: ListView(
physics: const ScrollPhysics(),
children: <Widget>[
Text(
'Quiz ${currentQuizIndex + 1}/${quizList.length}',
style: const TextStyle(fontSize: 20),
textAlign: TextAlign.center,
),
TeXView(
child: TeXViewColumn(children: [
TeXViewDocument(quizList[currentQuizIndex].statement,
style:
const TeXViewStyle(textAlign: TeXViewTextAlign.center)),
TeXViewGroup(
children: quizList[currentQuizIndex]
.options
.map((QuizOption option) {
return TeXViewGroupItem(
rippleEffect: false,
id: option.id,
child: TeXViewDocument(option.option,
style: const TeXViewStyle(
padding: TeXViewPadding.all(10))));
}).toList(),
selectedItemStyle: TeXViewStyle(
borderRadius: const TeXViewBorderRadius.all(10),
border: TeXViewBorder.all(TeXViewBorderDecoration(
borderWidth: 3, borderColor: Colors.green[900])),
margin: const TeXViewMargin.all(10)),
normalItemStyle:
const TeXViewStyle(margin: TeXViewMargin.all(10)),
onTap: (id) {
selectedOptionId = id;
setState(() {
isWrong = false;
});
})
]),
style: const TeXViewStyle(
margin: TeXViewMargin.all(5),
padding: TeXViewPadding.all(10),
borderRadius: TeXViewBorderRadius.all(10),
border: TeXViewBorder.all(
TeXViewBorderDecoration(
borderColor: Colors.blue,
borderStyle: TeXViewBorderStyle.solid,
borderWidth: 5),
),
backgroundColor: Colors.white,
),
// loadingWidgetBuilder: (context) {
// return const Center(
// child: CircularProgressIndicator(),
// );
// },
),
if (isWrong)
const Padding(
padding: EdgeInsets.all(20),
child: Text(
"Wrong answer!!! Please choose a correct option.",
textAlign: TextAlign.center,
style: TextStyle(fontSize: 18, color: Colors.red),
),
),
Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
setState(() {
if (currentQuizIndex > 0) {
selectedOptionId = "";
currentQuizIndex--;
}
});
},
child: const Text("Previous"),
),
ElevatedButton(
onPressed: () {
setState(() {
if (selectedOptionId ==
quizList[currentQuizIndex].correctOptionId) {
selectedOptionId = "";
if (currentQuizIndex != quizList.length - 1) {
currentQuizIndex++;
}
} else {
isWrong = true;
}
});
},
child: const Text("Next"),
),
],
)
],
),
);
}
}