Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import 'package:flutter/material.dart';
  2. import 'question_bank.dart';
  3.  
  4. QuestionBank questionBank = QuestionBank();
  5.  
  6. void main() => runApp(Quizzler());
  7.  
  8. class Quizzler extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. home: Scaffold(
  13. backgroundColor: Colors.grey.shade900,
  14. body: SafeArea(
  15. child: Padding(
  16. padding: EdgeInsets.symmetric(horizontal: 10.0),
  17. child: QuizPage(),
  18. ),
  19. ),
  20. ),
  21. );
  22. }
  23. }
  24.  
  25. class QuizPage extends StatefulWidget {
  26. @override
  27. _QuizPageState createState() => _QuizPageState();
  28. }
  29.  
  30. class _QuizPageState extends State<QuizPage> {
  31. List<Icon> scoreKeeper = [];
  32.  
  33. void checkAnswer(bool userAnswer) {
  34. bool correctAnswer = questionBank.getCorrectAnswer();
  35.  
  36. setState(() {
  37. if (userAnswer == correctAnswer) {
  38. scoreKeeper.add(
  39. Icon(
  40. Icons.check,
  41. color: Colors.green,
  42. ),
  43. );
  44. } else {
  45. scoreKeeper.add(
  46. Icon(
  47. Icons.close,
  48. color: Colors.red,
  49. ),
  50. );
  51. }
  52. questionBank.nextQuestion();
  53. });
  54. }
  55.  
  56. @override
  57. Widget build(BuildContext context) {
  58. return Column(
  59. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  60. crossAxisAlignment: CrossAxisAlignment.stretch,
  61. children: <Widget>[
  62. Expanded(
  63. flex: 5,
  64. child: QuestionText(),
  65. ),
  66. Expanded(
  67. child: buildAnswerButton(
  68. buttonColour: Colors.green,
  69. buttonText: 'True',
  70. userAnswer: true,
  71. ),
  72. ),
  73. Expanded(
  74. child: buildAnswerButton(
  75. buttonColour: Colors.red,
  76. buttonText: 'False',
  77. userAnswer: false,
  78. )),
  79. Row(
  80. children: scoreKeeper,
  81. ),
  82. ],
  83. );
  84. }
  85.  
  86. // Refactored as Method
  87. Widget buildAnswerButton({
  88. Color buttonColour,
  89. String buttonText,
  90. bool userAnswer,
  91. }) {
  92. return Padding(
  93. padding: EdgeInsets.all(15.0),
  94. child: FlatButton(
  95. textColor: Colors.white,
  96. color: buttonColour,
  97. child: Text(
  98. buttonText,
  99. style: TextStyle(
  100. color: Colors.white,
  101. fontSize: 20.0,
  102. ),
  103. ),
  104. onPressed: () {
  105. //The user picked true.
  106. checkAnswer(userAnswer);
  107. },
  108. ),
  109. );
  110. }
  111. }
  112.  
  113. //Refactored as Widget
  114. class QuestionText extends StatelessWidget {
  115. const QuestionText({
  116. Key key,
  117. }) : super(key: key);
  118.  
  119. @override
  120. Widget build(BuildContext context) {
  121. return Padding(
  122. padding: EdgeInsets.all(10.0),
  123. child: Center(
  124. child: Text(
  125. questionBank.getQuestionText(),
  126. textAlign: TextAlign.center,
  127. style: TextStyle(
  128. fontSize: 25.0,
  129. color: Colors.white,
  130. ),
  131. ),
  132. ),
  133. );
  134. }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement