Created
February 9, 2021 04:24
-
-
Save Chico-Chen/fd0932e929b97c559049429cdca94c8d 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
//flutter expose to use | |
import 'package:flutter/material.dart'; | |
import './result.dart'; | |
import './quiz.dart'; | |
//entry for the application | |
void main() => runApp(MyApp()); | |
//When external input data change, StatefulWidget should be rebuilt basically | |
//Why does it have two classes(MyApp, MyAppState) | |
class MyApp extends StatefulWidget { | |
@override | |
State<StatefulWidget> createState() { | |
return _MyAppState(); | |
} | |
} | |
//State is a generic class, it's attach to MyApp widget | |
//Implementation: When questionIndex changes, StatefulWidget 'MyApp' also recreates | |
//MyAppState is a persistant | |
//underscore turns a public class into private | |
class _MyAppState extends State<MyApp> { | |
final _questions = const [ | |
{ | |
'questionText': 'What\'s your favorite color?', | |
'answers': [ | |
{'text': 'Black', 'score': 10}, | |
{'text': 'Red', 'score': 5}, | |
{'text': 'Green', 'score': 3}, | |
{'text': 'White', 'score': 1}, | |
], | |
}, | |
{ | |
'questionText': 'What\'s your favorite animal?', | |
'answers': [ | |
{'text': 'Rabbit', 'score': 3}, | |
{'text': 'Cat', 'score': 11}, | |
{'text': 'Elephant', 'score': 5}, | |
{'text': 'Lion', 'score': 9}, | |
], | |
}, | |
{ | |
'questionText': 'Who\'s your favorite instructor?', | |
'answers': [ | |
{'text': 'Peter', 'score': 1}, | |
{'text': 'Max', 'score': 1}, | |
{'text': 'Mohsen', 'score': 1}, | |
{'text': 'Robin', 'score': 1}, | |
], | |
}, | |
]; | |
var _questionIndex = 0; | |
var _totalScore = 0; | |
//restart the quiz | |
void _resetQuiz() { | |
setState(() { | |
_questionIndex = 0; | |
_totalScore = 0; | |
}); | |
} | |
void _answerQuestion(int score) { | |
// var aBool = true; | |
// aBool = false; | |
_totalScore += score; | |
setState(() { | |
_questionIndex = _questionIndex + 1; | |
}); | |
print(_questionIndex); | |
if (_questionIndex < _questions.length) { | |
print('We have more questions!'); | |
} else { | |
print('No more questions!'); | |
} | |
} | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: Scaffold( | |
appBar: AppBar( | |
title: Text('My First App'), | |
), | |
body: _questionIndex < _questions.length | |
? Quiz( | |
answerQuestion: _answerQuestion, | |
questionIndex: _questionIndex, | |
questions: _questions, | |
) | |
: Result(_totalScore, _resetQuiz), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment