-
-
Notifications
You must be signed in to change notification settings - Fork 1
Description
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
static const String _title = 'MCQ Quiz';
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: _title,
home: Scaffold(
appBar: AppBar(
title: const Text(_title),
centerTitle: true,
),
body: Center(
child: MyStatefulWidget(),
),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({super.key});
String question = "Which state has the capital Lucknow?";
int questionno = -1;
int correctanswers = 0;
bool isTestOver = false;
List questions = QuestionArray.questions;
Question? currentquestion;
List scores = [];
void setQuestion(bool b) {
//isTestOver=false;
//questionno=-1;
//scores.clear();
if (isTestOver) return;
if (questionno == -1) {
questionno++;
currentquestion = questions[questionno];
return;
}
if (questionno >= questions.length - 1) {
addResult(b);
isTestOver = true;
return;
}
addResult(b);
questionno++;
if (questionno <= questions.length - 1) {
currentquestion = questions[questionno];
}
}
void addResult(bool b) {
bool iscorrect = b == currentquestion!.correctAnswer;
//scores.clear();
if (iscorrect) {
correctanswers++;
scores.add(const Icon(Icons.check, color: Colors.green));
} else {
scores.add(const Icon(Icons.close, color: Colors.red));
}
}
@OverRide
State createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State {
int result = 0;
@OverRide
Widget build(BuildContext context) {
return Column(
children: [
Expanded(child: Text("Result $result")),
Expanded(child: Text(widget.question)),
Row(
children: [
Expanded(
child: ListTile(
title: const Text('Uttar Pradesh'),
leading: Radio(
value: 1,
groupValue: result,
onChanged: (int? value) {
setState(() {
result = value!;
});
},
),
),
),
Expanded(
child: ListTile(
title: const Text('Bihar'),
leading: Radio(
value: 3,
groupValue: result,
onChanged: (int? value) {
setState(() {
result = value!;
});
},
),
),
),
],
),
Row(
children: [
Expanded(
child: ListTile(
title: const Text('Punjab'),
leading: Radio(
value: 2,
groupValue: result,
onChanged: (int? value) {
setState(() {
result = (value == null) ? 0 : value;
});
},
),
),
),
Expanded(
child: ListTile(
title: const Text('Himachal Pradesh'),
leading: Radio(
value: 4,
groupValue: result,
onChanged: (int? value) {
setState(() {
result = value!;
});
},
),
),
),
],
),
SizedBox(
child: Padding(
padding: const EdgeInsets.all(15.0),
child: ElevatedButton(
style: ElevatedButton.styleFrom(
primary: Colors.teal,
minimumSize: const Size.fromHeight(5),
),
child: const Text(
'Submit',
style: TextStyle(
fontSize: 20.0,
color: Colors.white,
),
),
onPressed: () {
print("Submitted False");
},
),
),
),
],
);
}
}
class QuestionArray {
static List questions = [
Question("What is the capital of UP?", "Lucknow", "Delhi", "Tokyo",
"Sundarpur", 1),
Question("Who is the prime minister of Japan?", "Donald Trump",
"Puspha Kamal Dahal", "Bhimsen Thapa", "Fumio Kishida", 1),
Question("What is the world largest Economy?", "India", "USA", "Japan",
"Germany", 2),
Question("The World,s best Education system", "United States", "Australia",
"United Kingdom", "Canada", 3),
];
}
class Question {
String question = "";
String opt1 = "", opt2 = "", opt3 = "", opt4 = "";
int correctAnswer = 0;
Question(this.question, this.opt1, this.opt2, this.opt3, this.opt4,
this.correctAnswer);
}