Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
This was a pretty long module,
I now want to wrap it up or bring it to an end by making this quiz a bit more realistic and showing
different results based on which answers we chose.
Now of course, you could now make this extremely complex,
I will make it relatively simple though.
What I want to do is I want to turn each answer here also into a map, by wrapping it with curly braces
and then we'll of course have our text or answer text or whatever you want to name it
and now that's important, you think I want to assign a score to this answer because we have kind of a
personality quiz here, so we can calculate an overall score of the personality of the person who takes
our quiz.
Now let's say and that's of course just my logic here,
the higher the score, the more strange or negative a person is and
therefore if you for example select black as your favorite color, I'll give you a score of 10,
on the other hand here for red where then red is our text,
we might go with a score of let's say 6 or 5 because red is more positive than black in my world at
least. Now green is even more positive because green is the color of hope
and of course, feel free to assign your own scores to fit your own personal judgment of these colors
but I'll gave green a color of three
and white is of course the color of innocence,
so here for white, I'll actually give that a score of let's say 1.
So with that, these are my answers,
they're now all maps, not just text, they've got a text key but then they also got scores.
Now of course, we can do the same here for our animals and I will now fast forward here,
of course feel free to assign your own scores or take the ones you find attached, attached you basically
find that questions list
now with all my finished scores assigned and we can then proceed with that. So with the finished
list here,
now of course when I pass down my questions here into the quiz, inside of the quiz widget where I build
these answer widgets, the answer I'm forwarding here is no longer just a string, that is incorrect instead
each answer I'm looking at is of course itself
now a map, so we should change this, a map where we have a string key and then objects, various values
because we have in our answers, we have both text and we have numbers. So therefore here when I map, answer
now is also a map with various values and therefore here I'm of course interested in my text, in the
answer text which I know will be a string but I will also need the score to calculate a total score
of course and for that here to answerQuestion,
so to that function which we have in the main.dart file, this _answerQuestion function here,
we actually need to get the score of the answer which was chosen.
So I expect a score here as an argument and this should be an integer, to calculate a total score.
Hence we should also add a total score, _totalScore here, a total score property in our overall
class and then here in answerQuestion, I want to increase or add to the total score the score that we
chose.
So total score should be equal to total score plus score By the way
if you have a setup like this, there also is a shortcut in Dart, instead of using equal old value plus
some new value,
you can also have plus equal and this will add this value to the old value before it then saves it back
into that variable.
So we add our score to the total score
now in answerQuestion but now we have to make sure that in the quiz.dart file here where we forward
answerQuestion to an answer, we actually make sure that we call answer questions with the chosen score
and at the moment where we only point a reference, a pointer at that function,
we're not doing that because with just a pointer,
we're not saying anything about the arguments that this function takes when it is called and by default,
Flutter will call it without any arguments because we must not forget that our function here is bound
to onPressed and onPressed takes a function without arguments.
So what do we do? In quiz.dart where I build my answer widgets, where I forward that pointer at answer
question, instead of forwarding this, I can forward an anonymous function and here I use this one line
arrow function syntax which I also showed you earlier.
Now by doing this, I create a function on the fly and to the answer widget, I automatically only pass
the address to that function.
So that function is created on the fly, stored in memory and instead of storing it in a variable which
then holds the address which I could use here, I automatically get back the address,
so I do get the address here and I forward that address to answer.
Now the thing is therefore my answer widget still gets a function which takes no arguments right,
that is an empty list and that then does something.
Now here however on the right side of the arrow here, I'm in the function body of that anonymous function.
So here, I no longer want to just use the address, here I instead want to call answerQuestion
Why?
Because this is now not executed when Dart parses this, there it will only create this anonymous function
and store it in memory
but this is now executed when onPressed is triggered, right because it's an anonymous function where
I forward the address to answer, the address is then bound to the button here,
so when a button is pressed, this address is used to then execute this anonymous function. So this function
body here is only triggered when the button is pressed.
Therefore when it is pressed, I do want to call answerQuestion
and now since I call it here, I do have access to my answer map here, to my answer object and therefore
also to the score because every answer through which I'm looping here or through which I'm going here, every
answer is just a map with a text and a score and now I can access that score here. So I can now forward
answer score here to answerQuestion and I know that this will be an integer and therefore, answer
question here will receive my integer score,
here it is.
I hope this flow is clear,
it's really just about passing function references around and here we're creating such a function and
therefore also such a function reference on the fly. It only gets executed when the button is pressed though,
so when it is pressed I of course want to execute this function in here. So with that, I can forward the score
to my answer
question function here in main and I do increment the total score now.
I now want to forward that total score to the result widget so that I can show different results there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.