Skip to content

Commit

Permalink
separate out quiz and result, add results based on answer score
Browse files Browse the repository at this point in the history
  • Loading branch information
pduebel committed Oct 19, 2021
1 parent 1fba4e3 commit 14a18c9
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 26 deletions.
72 changes: 46 additions & 26 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';

import './questions.dart';
import './answer.dart';
import './quiz.dart';
import './result.dart';

void main() => runApp(MyApp());

Expand All @@ -14,43 +14,63 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
final _questions = const [
{
'questionText': 'What\'s your favourite colour?',
'answers': [
{'text': 'Black', 'score': 10},
{'text': 'Red', 'score': 5},
{'text': 'Green', 'score': 3},
{'text': 'Blue', 'score': 1}
]
},
{
'questionText': 'What\'s your favourite animal?',
'answers': [
{'text': 'Dog', 'score': 1},
{'text': 'Spider', 'score': 10},
{'text': 'Cat', 'score': 3},
{'text': 'Snake', 'score': 5}
]
},
{
'questionText': 'What\'s your favourite food?',
'answers': [
{'text': 'Pizza', 'score': 1},
{'text': 'Kebab', 'score': 5},
{'text': 'Chips', 'score': 3},
{'text': 'Coriander', 'score': 10}
]
}
];
var _questionIndex = 0;
var _totalScore = 0;

void _answerQuestion() {
void _answerQuestion(int score) {
setState(() {
_questionIndex++;
});
_totalScore += score;
print(_questionIndex);
if (_questionIndex < _questions.length) {
print('We have more questions!');
} else {
print('No more questions!');
}
}

@override
Widget build(BuildContext context) {
const questions = [
{
'questionText': 'What\'s your favourite colour?',
'answers': ['Black', 'Red', 'Green', 'White']
},
{
'questionText': 'What\'s your favourite animal?',
'answers': ['Rabbit', 'Snake', 'Elephant', 'Lion']
},
{
'questionText': 'What\'s your favourite food?',
'answers': ['Pizza', 'Curry', 'Chips', 'Ice Cream']
}
];
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Personality Quiz')),
body: Column(
children: [
Question((questions[_questionIndex]['questionText'] as String)),
...(questions[_questionIndex]['answers'] as List<String>)
.map((answer) {
return Answer(_answerQuestion, answer);
}).toList()
],
),
body: _questionIndex < _questions.length
? Quiz(
answerQuestion: _answerQuestion,
questionIndex: _questionIndex,
questions: _questions,
)
: Result(_totalScore),
),
);
}
Expand Down
29 changes: 29 additions & 0 deletions lib/quiz.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';

import './questions.dart';
import './answer.dart';

class Quiz extends StatelessWidget {
final List<Map<String, Object>> questions;
final int questionIndex;
final Function(int) answerQuestion;

Quiz({
required this.questions,
required this.answerQuestion,
required this.questionIndex,
});
@override
Widget build(BuildContext context) {
return Column(
children: [
Question((questions[questionIndex]['questionText'] as String)),
...(questions[questionIndex]['answers'] as List<Map<String, Object>>)
.map((answer) {
return Answer(() => answerQuestion(answer['score'] as int),
answer['text'] as String);
}).toList()
],
);
}
}
34 changes: 34 additions & 0 deletions lib/result.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:flutter/material.dart';

class Result extends StatelessWidget {
final int resultScore;

Result(this.resultScore);

String get resultPhrase {
String resultText;
if (resultScore <= 7) {
resultText = 'You are awesome!';
} else if (resultScore <= 13) {
resultText = 'Pretty likeable!';
} else {
resultText = 'You weirdo!';
}
return resultText;
}

@override
Widget build(BuildContext context) {
return Column(
children: [
Center(
child: Text(
resultPhrase,
style: const TextStyle(fontSize: 36, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
),
],
);
}
}

0 comments on commit 14a18c9

Please sign in to comment.