Advertisement
1keyup

Untitled

Dec 10th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1.  
  2. // ==========================================================================================
  3. // QUIZ.DART:
  4. // ------------------------------------------------------------------------------------------
  5.  
  6. import 'package:flutter/material.dart';
  7. import 'package:quiz/question.dart';
  8.  
  9. import 'answer.dart';
  10.  
  11. class Quiz extends StatelessWidget {
  12.  
  13. final List<Map<String, Object>> questions;
  14. final int questionIndex;
  15. final Function answerQuestion;
  16.  
  17. Quiz({
  18. @required this.questions,
  19. @required this.answerQuestion,
  20. @required this.questionIndex,
  21. });
  22.  
  23. @override
  24. Widget build(BuildContext context) {
  25. return Column(
  26. children: <Widget>[
  27. Question(
  28. questions[questionIndex]['questionText']
  29. ),
  30. ...(questions[questionIndex]['answers'] as List<Map<String, Object>>).map((answer){
  31. return Answer(() => answerQuestion(answer['score']), answer['text']);
  32. }).toList()
  33. ],
  34. );
  35. }
  36. }
  37.  
  38.  
  39. // ==========================================================================================
  40. // MAIN.DART:
  41. // ------------------------------------------------------------------------------------------
  42.  
  43.  
  44. import 'package:flutter/material.dart';
  45. import 'package:quiz/quiz.dart';
  46. import 'package:quiz/result.dart';
  47.  
  48.  
  49. void main() => runApp(MyApp());
  50.  
  51.  
  52. class MyApp extends StatefulWidget {
  53.  
  54. @override
  55. State<StatefulWidget> createState() {
  56. return _MyAppState();
  57. }
  58.  
  59. }
  60.  
  61. class _MyAppState extends State<MyApp> {
  62.  
  63. var _questionIndex = 0;
  64. var _totalScore = 0;
  65.  
  66. final _questions = [
  67. {
  68. 'questionText': 'What\'s your favorite color?',
  69. 'answers': [
  70. {'text': 'Red', 'scrore': 1},
  71. {'text': 'Green', 'scrore': 2},
  72. {'text': 'Blue', 'scrore': 3},
  73. {'text': 'Black', 'scrore': 4},
  74. ]
  75. },
  76. {
  77. 'questionText': 'What\'s your favorite animal?',
  78. 'answers': [
  79. {'text': 'Cat', 'score': 1},
  80. {'text': 'Dog', 'score': 2},
  81. {'text': 'Horse', 'score': 3}
  82. ]
  83. }
  84. ];
  85.  
  86. void _answerQuestion(int score){
  87.  
  88. _totalScore += score;
  89.  
  90. setState(() {
  91. _questionIndex += 1;
  92. });
  93.  
  94. print(_questionIndex);
  95. }
  96.  
  97. @override
  98. Widget build(BuildContext c) {
  99.  
  100. return MaterialApp(
  101.  
  102. home: Scaffold(
  103. appBar: AppBar(title: Text('Quiz App'),),
  104. body: _questionIndex < _questions.length
  105. ? Quiz(
  106. answerQuestion: _answerQuestion,
  107. questionIndex: _questionIndex,
  108. questions: _questions,
  109. )
  110. : Result(_totalScore)
  111. ),
  112.  
  113. );
  114. }
  115. }
  116.  
  117.  
  118.  
  119. // ==========================================================================================
  120. // ANSWER.DART:
  121. // ------------------------------------------------------------------------------------------
  122.  
  123.  
  124. import 'package:flutter/material.dart';
  125.  
  126. class Answer extends StatelessWidget {
  127.  
  128. final Function selectHandler;
  129. final String answerText;
  130.  
  131. Answer(this.selectHandler, this.answerText);
  132.  
  133. @override
  134. Widget build(BuildContext context) {
  135. return Container(
  136. width: double.infinity,
  137. margin: EdgeInsets.fromLTRB(20, 0, 20, 5),
  138.  
  139. child: RaisedButton(
  140. color: Colors.blue,
  141. textColor: Colors.white,
  142.  
  143. child: Text(answerText),
  144. onPressed: selectHandler,
  145. ),
  146. );
  147. }
  148. }
  149.  
  150. // ==========================================================================================
  151. // RESULT.DART:
  152. // ------------------------------------------------------------------------------------------
  153.  
  154. import 'package:flutter/material.dart';
  155.  
  156. class Result extends StatelessWidget {
  157.  
  158. final int resultScore;
  159.  
  160. Result(this.resultScore);
  161.  
  162. String get resultPhrase {
  163. String resultText;
  164.  
  165. if(resultScore <= 5){
  166. resultText = 'Could be better.';
  167. }else{
  168. resultText = 'You did it!';
  169. }
  170.  
  171. return resultText;
  172. }
  173.  
  174. @override
  175. Widget build(BuildContext context) {
  176. return Center(
  177. child: Text(resultPhrase,
  178. style: TextStyle(
  179. fontSize: 36,
  180. fontWeight: FontWeight.bold
  181. ),
  182. ),
  183. );
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement