Quiz App:
main.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:quiz_app/home.dart';
import 'package:quiz_app/quizscreen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(),
home: Splashscreen(),
);
}
}
class Splashscreen extends StatefulWidget {
@override
_SplashscreenState createState() => _SplashscreenState();
}
class _SplashscreenState extends State<Splashscreen> {
@override
void initState() {
super.initState();
Timer(Duration(seconds: 4), () {
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (context) => home()));
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset(
"images/il.png",
height: 120,
width: 120,
),
],
),
// child: Text(
// "Insta Quiz",
// style: TextStyle(
// fontSize: 24,
// ),
// ),
),
);
}
}
home.dart
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:argon_buttons_flutter/argon_buttons_flutter.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:quiz_app/quizscreen.dart';
class home extends StatefulWidget {
@override
_homeState createState() => _homeState();
}
class _homeState extends State<home> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: 500,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage(
"images/qb.jpg",
),
fit: BoxFit.cover,
),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 350,
),
// Image.asset(
// "images/m.png",
// fit: BoxFit.cover,
// ),
ArgonTimerButton(
height: 50,
width: MediaQuery.of(context).size.width * 0.45,
onTap: (startTimer, btnState) {
if (btnState == ButtonState.Idle) {
startTimer(5);
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => quizscreen()));
}
},
child: Text(
"Play Now",
style: TextStyle(
color: Colors.black,
fontSize: 24,
fontWeight: FontWeight.w700),
),
loader: (timeLeft) {
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(50)),
margin: EdgeInsets.all(5),
alignment: Alignment.center,
width: 40,
height: 40,
child: Text(
"",
style:
TextStyle(fontSize: 20, fontWeight: FontWeight.w700),
),
);
},
borderRadius: 15.0,
color: Colors.white70,
),
],
),
),
),
);
}
}Quiz screen.dart
import 'package:flutter/material.dart';
import 'package:quiz_app/answer.dart';
import 'package:quiz_app/brain.dart';
class quizscreen extends StatefulWidget {
@override
_quizscreenState createState() => _quizscreenState();
}
class _quizscreenState extends State<quizscreen> {
Brain brain = Brain();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(
child: Text(
"Insta Quiz",
style: TextStyle(
fontSize: 24,
),
)),
elevation: 0.0,
backgroundColor: Colors.transparent,
),
body: Container(
margin: EdgeInsets.symmetric(
vertical: 30,
horizontal: 16,
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
flex: 5,
child: Center(
child: Text(
brain.getQuestionText,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 25,
),
)),
),
Expanded(
child: AnswerButton(
text: "true",
color: Colors.green,
onPressed: () {
setState(() {
if (brain.getQuestionAnswer == true) {
brain.rightIcon();
} else {
brain.wrongIcon();
}
brain.Updatequesion(context);
});
},
),
),
Expanded(
child: AnswerButton(
text: "false",
color: Colors.red,
onPressed: () {
setState(() {
if (brain.getQuestionAnswer == false) {
brain.rightIcon();
} else {
brain.wrongIcon();
}
brain.Updatequesion(context);
});
},
),
),
SizedBox(
height: 40,
),
Text(
"Your Answer",
style: TextStyle(
color: Colors.yellow,
fontSize: 18,
),
),
Container(
margin: EdgeInsets.symmetric(
vertical: 15,
horizontal: 16,
),
child: Row(
children: brain.righticons,
),
),
],
),
),
);
}
}result.dart
import 'package:flutter/material.dart';
import 'package:quiz_app/answer.dart';
import 'package:quiz_app/brain.dart';
import 'package:quiz_app/home.dart';
import 'package:quiz_app/quizscreen.dart';
class result extends StatelessWidget {
Brain brain = Brain();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Center(
child: Text(
"Result",
style: TextStyle(
fontSize: 34,
),
)),
elevation: 0.0,
backgroundColor: Colors.transparent,
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 130,
),
Center(
child: Text(
"Thanks for Playing",
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 24,
),
),
),
SizedBox(
height: 50,
),
Icon(
Icons.thumb_up,
color: Colors.yellow,
size: 45,
),
SizedBox(
height: 130,
),
AnswerButton(
text: "Restart",
color: Colors.pink,
onPressed: () {
brain.restartquiz();
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => home(),
),
);
},
)
],
),
);
}
}brain.dart
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:quiz_app/question.dart';
import 'package:quiz_app/result.dart';
class Brain {
List<Question> _questions = [
Question(
"1. The hexadecimal number system contains digits from 1 - 15.", false),
Question(
"2. The Language that the computer can understand is called Machine Language.",
true),
Question("3. MS Word is a hardware.", false),
Question("4. CPU controls only input data of computer.", false),
Question("5. GNU / Linux is a open source operating system.", true),
Question("6. Twitter is an online social networking and blogging service.",
false),
];
List<Icon> _icons = [];
var _questionIndex = 0;
bool get getQuestionAnswer {
return _questions[_questionIndex].questionAnswer;
}
String get getQuestionText {
return _questions[_questionIndex].questionText;
}
void Updatequesion(context) {
if (_questionIndex < _questions.length - 1) {
_questionIndex++;
} else {
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => result(),
),
);
}
}
void restartquiz() {
_questionIndex = 0;
}
List<Icon> get righticons {
return _icons;
}
void rightIcon() {
_icons.add(Icon(
Icons.check,
color: Colors.green,
));
}
void wrongIcon() {
_icons.add(Icon(
Icons.close,
color: Colors.red,
));
}
}question.dart
class Question {
final String questionText;
final bool questionAnswer;
Question(this.questionText, this.questionAnswer);
}answer.dart
import 'package:flutter/material.dart';
class AnswerButton extends StatelessWidget {
final String text;
final Color color;
final Function onPressed;
AnswerButton({this.text, this.color, this.onPressed});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPressed,
child: Container(
height: 60,
width: double.infinity,
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(15),
),
margin: EdgeInsets.symmetric(horizontal: 20.0, vertical: 10.0),
child: Center(
child: Text(
text,
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 22,
),
)),
),
);
}
}
0 Comments