Created
March 14, 2019 16:48
-
-
Save angelabauer/7ee2a7060ab37af400dfbc0f327b4dc8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import 'package:flutter/material.dart'; | |
| import 'question_bank.dart'; | |
| QuestionBank questionBank = QuestionBank(); | |
| void main() => runApp(Quizzler()); | |
| class Quizzler extends StatelessWidget { | |
| @override | |
| Widget build(BuildContext context) { | |
| return MaterialApp( | |
| home: Scaffold( | |
| backgroundColor: Colors.grey.shade900, | |
| body: SafeArea( | |
| child: Padding( | |
| padding: EdgeInsets.symmetric(horizontal: 10.0), | |
| child: QuizPage(), | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } | |
| class QuizPage extends StatefulWidget { | |
| @override | |
| _QuizPageState createState() => _QuizPageState(); | |
| } | |
| class _QuizPageState extends State<QuizPage> { | |
| List<Icon> scoreKeeper = []; | |
| void checkAnswer(bool userAnswer) { | |
| bool correctAnswer = questionBank.getCorrectAnswer(); | |
| setState(() { | |
| if (userAnswer == correctAnswer) { | |
| scoreKeeper.add( | |
| Icon( | |
| Icons.check, | |
| color: Colors.green, | |
| ), | |
| ); | |
| } else { | |
| scoreKeeper.add( | |
| Icon( | |
| Icons.close, | |
| color: Colors.red, | |
| ), | |
| ); | |
| } | |
| questionBank.nextQuestion(); | |
| }); | |
| } | |
| @override | |
| Widget build(BuildContext context) { | |
| return Column( | |
| mainAxisAlignment: MainAxisAlignment.spaceBetween, | |
| crossAxisAlignment: CrossAxisAlignment.stretch, | |
| children: <Widget>[ | |
| Expanded( | |
| flex: 5, | |
| child: QuestionText(), | |
| ), | |
| Expanded( | |
| child: buildAnswerButton( | |
| buttonColour: Colors.green, | |
| buttonText: 'True', | |
| userAnswer: true, | |
| ), | |
| ), | |
| Expanded( | |
| child: buildAnswerButton( | |
| buttonColour: Colors.red, | |
| buttonText: 'False', | |
| userAnswer: false, | |
| )), | |
| Row( | |
| children: scoreKeeper, | |
| ), | |
| ], | |
| ); | |
| } | |
| // Refactored as Method | |
| Widget buildAnswerButton({ | |
| Color buttonColour, | |
| String buttonText, | |
| bool userAnswer, | |
| }) { | |
| return Padding( | |
| padding: EdgeInsets.all(15.0), | |
| child: FlatButton( | |
| textColor: Colors.white, | |
| color: buttonColour, | |
| child: Text( | |
| buttonText, | |
| style: TextStyle( | |
| color: Colors.white, | |
| fontSize: 20.0, | |
| ), | |
| ), | |
| onPressed: () { | |
| //The user picked true. | |
| checkAnswer(userAnswer); | |
| }, | |
| ), | |
| ); | |
| } | |
| } | |
| //Refactored as Widget | |
| class QuestionText extends StatelessWidget { | |
| const QuestionText({ | |
| Key key, | |
| }) : super(key: key); | |
| @override | |
| Widget build(BuildContext context) { | |
| return Padding( | |
| padding: EdgeInsets.all(10.0), | |
| child: Center( | |
| child: Text( | |
| questionBank.getQuestionText(), | |
| textAlign: TextAlign.center, | |
| style: TextStyle( | |
| fontSize: 25.0, | |
| color: Colors.white, | |
| ), | |
| ), | |
| ), | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment