-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquestion4.dart
More file actions
206 lines (174 loc) · 5.43 KB
/
question4.dart
File metadata and controls
206 lines (174 loc) · 5.43 KB
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
import 'dart:convert';
import 'dart:core';
import 'package:flutter/material.dart';
import 'package:remar_flutter_app/question_screens/global.dart';
bool isMonthValid = false;
class QuestionAnswer4Page extends StatefulWidget {
final String name;
final String image;
final String month;
final Function(String) onMonthSelected;
const QuestionAnswer4Page({
Key? key,
required this.name,
required this.image, required this.month, required this.onMonthSelected,
}) : super(key: key);
@override
_QuestionAnswerPage4State createState() => _QuestionAnswerPage4State();
}
class _QuestionAnswerPage4State extends State<QuestionAnswer4Page> {
String selectedArea = '';
List<String> answers = [];
String questionText = '';
String month = '';
final ScrollController _controller = ScrollController();
@override
void initState() {
super.initState();
loadQuestions();
}
void loadQuestions() async {
enableForwardNavigation = false;
// Load the JSON data from the file
String jsonString = await DefaultAssetBundle.of(context)
.loadString('assets/raw_eng/questions2Modified.json');
// Parse the JSON string into a list of objects
List<dynamic> jsonData = jsonDecode(jsonString);
// Extract data from the first question (question number 9)
Map<String, dynamic> firstQuestionData = jsonData[3];
// Set question text and answers list
setState(() {
questionText = firstQuestionData['questionText'];
// Extract answers from the answers map
List<dynamic> answerList = firstQuestionData['answers'];
// Cast answers to List<String>
answers = answerList.map((answer) => answer.toString()).toList();
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Image.asset(
widget.image,
width: 100,
height: 125,
),
Text(
widget.name,
style: const TextStyle(fontSize: 24),
),
],
),
SizedBox(
width: MediaQuery
.of(context)
.size
.width * 0.8,
height: MediaQuery
.of(context)
.size
.height * 0.15,
child: Text(questionText,
style: const TextStyle(fontSize: 16),
),
),
SizedBox(
height: MediaQuery
.of(context)
.size
.height * 0.4,
width: MediaQuery
.of(context)
.size
.width * 0.6,
child: Scrollbar(
controller: _controller,
child: ListView.separated(
controller: _controller,
itemCount: answers.length,
itemBuilder: (BuildContext context, int index) {
return buildAnswerButton(answers[index]);
}, separatorBuilder: (BuildContext context, int index) {
return const Divider();
},
),
),
),
], // Column children
), // Column
), // Scaffold
); // MaterialApp
}
Widget buildAnswerButton(String answer) {
bool isSelected = answer == selectedArea;
return GestureDetector(
onTap: () {
setState(() {
selectedArea = answer;
month = answer;
validateMonth(month);
if(isMonthValid == true) {
widget.onMonthSelected(month);
enableForwardNavigation = true;
}
Container(
color: isSelected ? Colors.white : null,
padding: const EdgeInsets.all(1.0),
margin: const EdgeInsets.symmetric(vertical: 1.0),
child: Text(
answer,
style: TextStyle(
color: isSelected ? Colors.white : Colors.black,
fontWeight: FontWeight.bold,
),
),
);
});
},
child: Container(
color: isSelected ? Colors.green : null,
padding: const EdgeInsets.all(1.0),
margin: const EdgeInsets.symmetric(vertical: 1.0),
child: Text(
answer,
style: TextStyle(
color: isSelected ? Colors.white : Colors.black,
fontWeight: FontWeight.bold,
),
),
),
);
}
}
void validateMonth(String month) {
var currentDate = DateTime.now();
var currentMonth = currentDate.month;
var selectedMonth =0;
switch (month) {
case "January" : (selectedMonth=1);
case "February" : (selectedMonth=2);
case "March" : (selectedMonth=3);
case "April" : (selectedMonth=4);
case "May" : (selectedMonth=5);
case "June" : (selectedMonth=6);
case "July" : (selectedMonth=7);
case "August" : (selectedMonth=8);
case "September" : (selectedMonth=9);
case "October" : (selectedMonth=10);
case "November" : (selectedMonth=11);
case "December" : (selectedMonth=12);
}
if(selectedMonth > currentMonth) {
isMonthValid = false;
}
else {
isMonthValid = true;
}
}