{

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: Read pubspec.yaml attributes (version) with examples

How to Create and build a Singleton Class Dart or Flutter example| Dart By Example

What is the difference between var and dynamic and final in dart?| Flutter By Example

How to: Check if Phone number is valid or not in Dart| Flutter By Example

Dart| Flutter How to read an image from a disk and resize a file

Dart/Flutter: How to write setter and getter fields or members variables in a class with Example

Dart| Flutter How to get extension name and MIME type of a file with example