I want to greet user when they visit my app
I have tried using TimeOfDay but it isn't working.
TimeOfDay now = TimeOfDay.now();
greetings(String greeting){
var greeting = now;
if(greeting <= '11: 59'){
return 'Morning';
} else if (greeting > '11:59' && <= '16:59'){
return 'Afternoon';
} else if (greeting > '16:59' && <= '23:59'){
return 'Afternoon';
} else {
return 'Morning';
}
}
Try using DateTime.now()
, for example:
String greeting() {
var hour = DateTime.now().hour;
if (hour < 12) {
return 'Morning';
}
if (hour < 17) {
return 'Afternoon';
}
return 'Evening';
}
Fetch the current time and Parse it and fetch only hours.
var timeNow = DateTime.now().hour;
Now, our timeNow a variable has the hour value in integer format we only need to check the condition and Change messages according to time like this
String greetingMessage(){
var timeNow = DateTime.now().hour;
if (timeNow <= 12) {
return 'Good Morning';
} else if ((timeNow > 12) && (timeNow <= 16)) {
return 'Good Afternoon';
} else if ((timeNow > 16) && (timeNow < 20)) {
return 'Good Evening';
} else {
return 'Good Night';
}
}
And use as
String greetingMes = greetingMessage();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With