import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
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': 'Snake', 'score': 11},
{'text': 'Elephant', 'score': 5},
{'text': 'Lion', 'score': 9},
],
},
{
'questionText': 'Who\'s your favorite instructor?',
'answers': [
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1},
{'text': 'Max', 'score': 1},
],
},
];
var _questionIndex = 0;
var _totalScore = 0;
void _resetQuiz() {
setState(() {
_questionIndex = 0;
_totalScore = 0;
});
}
void _answerQuestion(int score) {
_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
? QuizWidget( answerQuestion: _answerQuestion, questionIndex: _questionIndex, questions: _questions )
: ResultWidget(_totalScore, _resetQuiz),
),
);
}
}
class QuizWidget extends StatelessWidget {
final List<Map<String, Object>> questions;
final int questionIndex;
final Function answerQuestion;
QuizWidget({ @required this.questions, @required this.answerQuestion, @required this.questionIndex });
@override
Widget build(BuildContext context) {
return Column(
children: [
QuestionWidget(
questions[questionIndex]['questionText'],
),
...(questions[questionIndex]['answers'] as List).map((_answer) {
return AnswerWidget(() => answerQuestion(_answer['score']), _answer['text']);
}).toList()
],
);
}
}
class QuestionWidget extends StatelessWidget {
final String questionText;
QuestionWidget(this.questionText);
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
margin: EdgeInsets.all(10),
child: Text(
questionText,
style: TextStyle(fontSize: 28),
textAlign: TextAlign.center,
),
);
}
}
class AnswerWidget extends StatelessWidget {
final Function selectHandler;
final String answerText;
AnswerWidget(this.selectHandler, this.answerText);
@override
Widget build(BuildContext context) {
return Container(
width: double.infinity,
child: RaisedButton(
color: Colors.blue,
textColor: Colors.white,
child: Text(answerText),
onPressed: selectHandler,
),
);
}
}
class ResultWidget extends StatelessWidget {
final int resultScore;
final Function resetHandler;
ResultWidget(this.resultScore, this.resetHandler);
String get resultPhrase {
String resultText;
if (resultScore <= 8) {
resultText = 'You are awesome and innocent!';
} else if (resultScore <= 12) {
resultText = 'Pretty likeable!';
} else if (resultScore <= 16) {
resultText = 'You are ... strange?!';
} else {
resultText = 'You are so bad!';
}
return resultText;
}
@override
Widget build(BuildContext context) {
return Center(
child: Column(
children: <Widget>[
Text(
resultPhrase,
style: TextStyle(fontSize: 36, fontWeight: FontWeight.bold),
textAlign: TextAlign.center,
),
FlatButton(
child: Text(
'Restart Quiz!',
),
textColor: Colors.blue,
onPressed: resetHandler,
),
],
),
);
}
}
No comments:
Post a Comment