Created
October 9, 2019 05:38
-
-
Save McLarenCollege/162b20671ee8f47881fc78801c6d8b33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/cupertino.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(Quizlr()); | |
} | |
class Quizlr extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
backgroundColor: Colors.grey.shade900, | |
body: SafeArea( | |
child: Padding( | |
padding: EdgeInsets.symmetric(horizontal: 10.0), | |
child: QuizPage(), | |
), | |
), | |
)); | |
} | |
} | |
class QuizPage extends StatefulWidget { | |
@override | |
_QuizPageState createState() => _QuizPageState(); | |
} | |
class _QuizPageState extends State<QuizPage> { | |
List<Icon> scoreKeeper = []; | |
List<String> questions = [ | |
'Amartya Sen was awarded the Nobel prize for his contribution to Welfare Economics.', | |
'The Headquarters of the Southern Naval Command of the India Navy is located at Thiruvananthapuram.', | |
'There are 4 sessions of the Parliament each year: Spring, Summer, Autumn and Winter.' | |
]; | |
int currentQuestion = 0; | |
List<bool> answers = [true, false, false]; | |
void checkAnswer(bool userAnswer) { | |
setState(() { | |
if (userAnswer == answers[currentQuestion]) { | |
scoreKeeper.add(Icon( | |
Icons.check, | |
color: Colors.green, | |
)); | |
} else { | |
scoreKeeper.add(Icon( | |
Icons.close, | |
color: Colors.red, | |
)); | |
} | |
currentQuestion++; | |
}); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
crossAxisAlignment: CrossAxisAlignment.stretch, | |
children: <Widget>[ | |
Expanded( | |
flex: 5, | |
child: Padding( | |
padding: EdgeInsets.all(10.0), | |
child: Center( | |
child: Text( | |
questions[currentQuestion], | |
textAlign: TextAlign.center, | |
style: TextStyle( | |
fontSize: 24.0, | |
color: Colors.white, | |
), | |
), | |
), | |
), | |
), | |
Expanded( | |
child: Padding( | |
padding: EdgeInsets.all(10.0), | |
child: FlatButton( | |
textColor: Colors.white, | |
color: Colors.green, | |
child: Text( | |
'True', | |
style: TextStyle( | |
fontSize: 20.0, | |
color: Colors.white, | |
), | |
), | |
onPressed: () { | |
checkAnswer(true); | |
}), | |
), | |
), | |
Expanded( | |
child: Padding( | |
padding: EdgeInsets.all(10.0), | |
child: FlatButton( | |
textColor: Colors.white, | |
color: Colors.red, | |
child: Text( | |
'False', | |
style: TextStyle( | |
fontSize: 20.0, | |
color: Colors.white, | |
), | |
), | |
onPressed: () { | |
checkAnswer(false); | |
}, | |
), | |
), | |
), | |
Row( | |
children: scoreKeeper, | |
) | |
], | |
); | |
} | |
} | |
// Sample Questions and Answers | |
// Q.1 Amartya Sen was awarded the Nobel prize for his contribution to Welfare Economics., true | |
// Q.2 The Headquarters of the Southern Naval Command of the India Navy is located at Thiruvananthapuram., false | |
// Q.3 There are 4 sessions of the Parliament each year: Spring, Summer, Autumn and Winter., false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment