How to display greeting message on user time in Dart| Flutter by Example
In this article, You’ll find how to display greeting messages such as Good Morning, afternoon, or evening based on the user’s time in dart and flutter.
The following are steps to print the greeting message.
- First, Get an hour of user time in dart
- if user time is less than or equal to 12, Display
Good Morning
. - if user time is greater than 12 and less than 16, Display `Good Afternoon.
- if user time is greater than 16 and less than 20, Display
Good Evening
. - if user time is greater than 20 and less than 24, Display
Good Evening
.
Get the current date using DateTime.now()
. call DateTime.hour
property returns the hour of a day in the 24-hour format.
Check the given hour using if-else if conditional expression.
void main() {
var hour = DateTime.now().hour;
if (hour <= 12) {
print('Good Morning');
} else if ((hour > 12) && (hour <= 16)) {
print('Good Afternoon');
} else if ((hour > 16) && (hour < 20)) {
print('Good Evening');
} else {
print('Good Night');
}
}
Output:
Good Morning