Quiz App questions from local file

 main.dart

import 'package:flutter/material.dart'; 


import './quiz.dart'; 

import './result.dart'; 


void main() => runApp(MyApp()); 


class MyApp extends StatefulWidget { 

@override 

State<StatefulWidget> createState() { 

return _MyAppState(); 


class _MyAppState extends State<MyApp> { 

final _questions = const [ 

'questionText': 'Q1. Who created Flutter?', 

'answers': [ 

{'text': 'Facebook', 'score': -2}, 

{'text': 'Adobe', 'score': -2}, 

{'text': 'Google', 'score': 10}, 

{'text': 'Microsoft', 'score': -2}, 

], 

}, 

'questionText': 'Q2. What is Flutter?', 

'answers': [ 

{'text': 'Android Development Kit', 'score': -2}, 

{'text': 'IOS Development Kit', 'score': -2}, 

{'text': 'Web Development Kit', 'score': -2}, 

'text': 

'SDK to build beautiful IOS, Android, Web & Desktop Native Apps', 

'score': 10 

}, 

], 

}, 

'questionText': ' Q3. Which programing language is used by Flutter', 

'answers': [ 

{'text': 'Ruby', 'score': -2}, 

{'text': 'Dart', 'score': 10}, 

{'text': 'C++', 'score': -2}, 

{'text': 'Kotlin', 'score': -2}, 

], 

}, 

'questionText': 'Q4. Who created Dart programing language?', 

'answers': [ 

{'text': 'Lars Bak and Kasper Lund', 'score': 10}, 

{'text': 'Brendan Eich', 'score': -2}, 

{'text': 'Bjarne Stroustrup', 'score': -2}, 

{'text': 'Jeremy Ashkenas', 'score': -2}, 

], 

}, 

'questionText': 

'Q5. Is Flutter for Web and Desktop available in stable version?', 

'answers': [ 

'text': 'Yes', 

'score': -2, 

}, 

{'text': 'No', 'score': 10}, 

], 

}, 

]; 


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('Quiz App'), 

backgroundColor: Colors.blueaccent, 

), 

body: Padding( 

padding: const EdgeInsets.all(30.0), 

child: _questionIndex < _questions.length 

? Quiz( 

answerQuestion: _answerQuestion, 

questionIndex: _questionIndex, 

questions: _questions, 

) //Quiz 

: Result(_totalScore, _resetQuiz), 

), //Padding 

), //Scaffold 

debugShowCheckedModeBanner: false, 

); //MaterialApp 





*******************************************************

quiz.dart

import 'package:flutter/material.dart'; 

import './answer.dart'; 
import './question.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: [ 
Question( 
questions[questionIndex]['questionText'], 
), //Question 
...(questions[questionIndex]['answers'] as List<Map<String, Object>>) 
.map((answer) { 
return Answer(() => answerQuestion(answer['score']), answer['text']); 
}).toList() 
], 
); //Column 

*******************************************************
question.dart

import 'package:flutter/material.dart'; 

class Question extends StatelessWidget { 
final String questionText; 

Question(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, 
), //Text 
); //Contaier 

*******************************************************
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, 
child: RaisedButton( 
color: Color(0xFF00E676), 
textColor: Colors.white, 
child: Text(answerText), 
onPressed: selectHandler, 
), //RaisedButton 
); //Container 
*****************************************************

result.dart
import 'package:flutter/material.dart'; 

class Result extends StatelessWidget { 
final int resultScore; 
final Function resetHandler; 

Result(this.resultScore, this.resetHandler); 

//Remark Logic 
String get resultPhrase { 
String resultText; 
if (resultScore >= 41) { 
resultText = 'You are awesome!'; 
print(resultScore); 
} else if (resultScore >= 31) { 
resultText = 'Pretty likeable!'; 
print(resultScore); 
} else if (resultScore >= 21) { 
resultText = 'You need to work more!'; 
} else if (resultScore >= 1) { 
resultText = 'You need to work hard!'; 
} else { 
resultText = 'This is a poor score!'; 
print(resultScore); 
return resultText; 

@override 
Widget build(BuildContext context) { 
return Center( 
child: Column( 
mainAxisAlignment: MainAxisAlignment.center, 
children: <Widget>[ 
Text( 
resultPhrase, 
style: TextStyle(fontSize: 26, fontWeight: FontWeight.bold), 
textAlign: TextAlign.center, 
), //Text 
Text( 
'Score ' '$resultScore', 
style: TextStyle(fontSize: 36, fontWeight: FontWeight.bold), 
textAlign: TextAlign.center, 
), //Text 
FlatButton( 
child: Text( 
'Restart Quiz!', 
), //Text 
textColor: Colors.blue, 
onPressed: resetHandler, 
), //FlatButton 
], //<Widget>[] 
), //Column 
); //Center 
**********************************************************

Comments

Popular posts from this blog

Create table in mysql database in phpmyadmin panel

Flutter Scratch Card