{

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.

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 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
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example