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
So in result here,
let's add a final property, an int, resultScore or whatever you want to name it,
let's add our constructor and now as a positional argument, though of course you could always use a named
argument if you prefer that
but here I will take this as a positional argument,
my score.
So when we use the result widget here, we now have to forward the score and that of course now is our
total score.
So now the total score which we calculate here in the MyAppState class with the help of our total score
property
and the answerQuestion function, that total score is now forwarded to result and in result, we now
receive this here in the constructor and we automatically, thanks to the shortcut here, store it in the
resultScore property.
Now I just want to output different texts here based on my results score. Now first of all, let's assign
some styling here, we did this earlier, I will do this with the help of the TextStyle class here and the
style argument on my text widget and I want to set the font size here, to let's say 36 and also set the
FontWeight, we haven't done that before to FontWeight.bold. Again FontWeight is here a class and
that is a static property,
again FontWeight therefore is basically a utility, a grouper class which groups a couple of predefined
values and that is basically a value that instructs Flutter to turn this text to bold text.
Still we need different texts and we get different ways of now generating this,
I like the way of adding a getter here. A getter here is a special type of property,
it's basically a mixture of property and method.
You create a getter by first of all defining which type of value you want to get,
you want to derive and that would be a string because I want to get some text,
then you add the get keyword and then any name you want, like resultPhrase.
Now unlike in a method, you now don't add parentheses because a getter is like a method that can never
receive any arguments.
You do add a body though and in that body, you now have to return a string,
you always have to return something in a getter, you use it however like a normal property.
So down there instead of you did it,
I will use resultPhrase and I don't use it with parentheses to execute it like a function,
I'm also not using it as the address of a function because this is no function,
this is simply a Dart feature,
it's a normal property but the value is calculated dynamically and here, the value I want to calculate
is calculated based on the result score.
So in here, in my body of that getter, I'll create a new variable, result
text to avoid name clashes here and I'll assign a default of you did it but then I want to add an if
statement and check if my results score is let's say smaller or equal than 8.
If that is true, then here between the curly braces, I'll set result text equal to you are awesome
and innocent.
So I'm overwriting result text with that new value here,
else if and that's now new, I mentioned it earlier,
we haven't used it thus far,
it's an else statement which kicks in if that condition is not satisfied
but unlike the normal else statement, it does now not blindly execute the code between the curly braces
but it checks another condition first
and here I check if results score is smaller or equal than 12.
So if it's not smaller or equal than 8, then maybe it's smaller or equal than 12.
In that case I would set my result text to pretty likable, with an extra e though. If we don't
make it into there, we can check if the result score is smaller or equal than 16 maybe
and remember that in the way I designed this quiz, higher scores mean a more negative personality.
So in that case, I will set result text to
you are strange and then in all other cases, I will set the result text to
you are so bad.
Now of course since I have that overall else case here, we don't really need an initial value here because
we'll never keep that, we'll certainly at least make it into this else block here.
So we could also get rid of that and in that case as you learned, it's a good practice to define the type
that will be stored in result text in the end which is a string.
Now here in the end, I do return that result text and therefore now when we use resultPhrase down there,
that should be based on the result score we're getting and we should be outputting different results
based on the choice the user made. So let's reload this application here and now let's go for a
negative personality,
so I like black,
I like snakes and here everything is right and we see you are so bad.
Now that's good,
let's restart the app with the green button here.
Of course, it would be nice of a button here but for the moment, we don't have that.
So let's wait for that to reload
and now let's go for a good personality,
white and then I like rabbits
and of course I like myself,
you are awesome and innocent. So that's nice,
one thing of course I notice is the text itself is not centered, so we can quickly do that in the
result.dart file, here on the text widget, we can set textAlign to TextAlign.center, something we also
did earlier and now if we save that, it is centered.
So that's pretty sweet and that's working as it should
as it seems like, we're now showing different results based on different choices and now we have
maybe not the most awesome application we ever built but a realistic application that we could
fine tune and already deploy to the app stores if we wished to do that.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.