Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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,
- ),
- ),
- ),
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement