Quiz (True or False)
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:googlelogin23/Widget/textwidget.dart';
import 'package:googlelogin23/quiz_home.dart';
void main(){
runApp(AviQuiz());
}
class AviQuiz extends StatelessWidget {
const AviQuiz({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.teal,
title: const Card(child: TextWidget(
text: "Avi Learner & Quizer",
textSize: 25,
fontWeight: FontWeight.bold,
color: Colors.red,
maxLine: 1,
align: TextAlign.left),
color: Colors.teal,),
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(10),
child: QuizHome(),
)
),
),
);
}
}
import 'package:flutter/Material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:googlelogin23/Widget/textwidget.dart';
class QuizHome extends StatefulWidget {
const QuizHome({super.key});
@override
State<QuizHome> createState() => _QuizHomeState();
}
class _QuizHomeState extends State<QuizHome> {
List<Widget> scores = [];
void addResult(bool b) {
{
b=(b==currentquestion!.correctanswer);
if (b) {
scores.add(Icon(
Icons.check,
color: Colors.green,
));
} else {
scores.add(Icon(
Icons.close,
color: Colors.red,
));
}
}
}
void setQuestion(bool b){
if(questionno>=0)
addResult(b);
if(questionno>=lastquestion){
currectquestiontext="Text Over";
return;
}
questionno++;
currentquestion=question[questionno];
//print(currentquestion);
currectquestiontext=""+currentquestion!.question;
//print(currectquestiontext);
}
String currectquestiontext="Press any button to start quiz";
int questionno=-1;
int lastquestion=3;
Question? currentquestion;
List<Question> question=[
Question(question: "Ram is good girl",correctanswer: false),
Question(question: "C is programming language",correctanswer: true),
Question(question: "Varanasi in the bihar",correctanswer: false),
Question(question: "Mr x is a gamer",correctanswer: true),
];
@override
Widget build(BuildContext context) {
return Column(
children: [
Expanded(
flex: 5,
child: Padding(
padding: const EdgeInsets.all(10),
child: Center(
child: TextWidget(
text: currectquestiontext,//"Question will come here",
textSize: 20,
fontWeight: FontWeight.w500,
color: Colors.black,
maxLine: 2,
align: TextAlign.start),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(10),
child: ElevatedButton(
child: const TextWidget(
text: "True",
textSize: 20,
fontWeight: FontWeight.w500,
color: Colors.black,
maxLine: 2,
align: TextAlign.start),
onPressed: () {
print("Submitted true");
//addResult(true);
setState(() {
setQuestion(true);
});
},
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(10),
child: ElevatedButton(
child: const TextWidget(
text: "False",
textSize: 20,
fontWeight: FontWeight.w500,
color: Colors.black,
maxLine: 2,
align: TextAlign.start),
onPressed: () {
print("Submitted true");
//addResult(false);
setState(() {
setQuestion(false);
});
},
),
),
),
// Todo : Add a row here
Row(
children: scores,
)
],
);
}
}
class Question{
final String question;
final bool correctanswer;
Question({
required this.question,required this.correctanswer});
}
import 'package:flutter/material.dart';
class TextWidget extends StatelessWidget{
final String text;
final double textSize;
final FontWeight fontWeight;
final Color color;
final int maxLine;
final TextAlign align;
// TextOverflow textOverflow;
const TextWidget({
Key? key,
required this.text,
required this.textSize,
required this.fontWeight,
required this.color,
required this.maxLine,
required this.align,
}):super(key: key);
@override
Widget build(BuildContext context){
return Text(
text,
overflow: TextOverflow.ellipsis,
maxLines: maxLine,
textAlign: align,
style: Theme.of(context).textTheme.headline1!.copyWith(
fontSize: textSize,
fontWeight: fontWeight,
color: color
),
);
}
}
No comments:
Post a Comment