Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==========================================================================================
- // QUIZ.DART:
- // ------------------------------------------------------------------------------------------
- import 'package:flutter/material.dart';
- import 'package:quiz/question.dart';
- import 'answer.dart';
- class Quiz extends StatelessWidget {
- final List<Map<String, Object>> questions;
- final int questionIndex;
- final Function answerQuestion;
- Quiz({
- @required this.questions,
- @required this.answerQuestion,
- @required this.questionIndex,
- });
- @override
- Widget build(BuildContext context) {
- return Column(
- children: <Widget>[
- Question(
- questions[questionIndex]['questionText']
- ),
- ...(questions[questionIndex]['answers'] as List<Map<String, Object>>).map((answer){
- return Answer(() => answerQuestion(answer['score']), answer['text']);
- }).toList()
- ],
- );
- }
- }
- // ==========================================================================================
- // MAIN.DART:
- // ------------------------------------------------------------------------------------------
- import 'package:flutter/material.dart';
- import 'package:quiz/quiz.dart';
- import 'package:quiz/result.dart';
- void main() => runApp(MyApp());
- class MyApp extends StatefulWidget {
- @override
- State<StatefulWidget> createState() {
- return _MyAppState();
- }
- }
- class _MyAppState extends State<MyApp> {
- var _questionIndex = 0;
- var _totalScore = 0;
- final _questions = [
- {
- 'questionText': 'What\'s your favorite color?',
- 'answers': [
- {'text': 'Red', 'scrore': 1},
- {'text': 'Green', 'scrore': 2},
- {'text': 'Blue', 'scrore': 3},
- {'text': 'Black', 'scrore': 4},
- ]
- },
- {
- 'questionText': 'What\'s your favorite animal?',
- 'answers': [
- {'text': 'Cat', 'score': 1},
- {'text': 'Dog', 'score': 2},
- {'text': 'Horse', 'score': 3}
- ]
- }
- ];
- void _answerQuestion(int score){
- _totalScore += score;
- setState(() {
- _questionIndex += 1;
- });
- print(_questionIndex);
- }
- @override
- Widget build(BuildContext c) {
- return MaterialApp(
- home: Scaffold(
- appBar: AppBar(title: Text('Quiz App'),),
- body: _questionIndex < _questions.length
- ? Quiz(
- answerQuestion: _answerQuestion,
- questionIndex: _questionIndex,
- questions: _questions,
- )
- : Result(_totalScore)
- ),
- );
- }
- }
- // ==========================================================================================
- // ANSWER.DART:
- // ------------------------------------------------------------------------------------------
- import 'package:flutter/material.dart';
- class Answer extends StatelessWidget {
- final Function selectHandler;
- final String answerText;
- Answer(this.selectHandler, this.answerText);
- @override
- Widget build(BuildContext context) {
- return Container(
- width: double.infinity,
- margin: EdgeInsets.fromLTRB(20, 0, 20, 5),
- child: RaisedButton(
- color: Colors.blue,
- textColor: Colors.white,
- child: Text(answerText),
- onPressed: selectHandler,
- ),
- );
- }
- }
- // ==========================================================================================
- // RESULT.DART:
- // ------------------------------------------------------------------------------------------
- import 'package:flutter/material.dart';
- class Result extends StatelessWidget {
- final int resultScore;
- Result(this.resultScore);
- String get resultPhrase {
- String resultText;
- if(resultScore <= 5){
- resultText = 'Could be better.';
- }else{
- resultText = 'You did it!';
- }
- return resultText;
- }
- @override
- Widget build(BuildContext context) {
- return Center(
- child: Text(resultPhrase,
- style: TextStyle(
- fontSize: 36,
- fontWeight: FontWeight.bold
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement