I want to show up greeting text something like (Goodmorning, Good afternoon, Good evening) in a text widget between the times of 6 hours intervals in flutter
I’ve been trying to come out with how to…but still nothing so far. If someone can help me out with a code of such. Thanks
2
Answers
You may use the DateTime.now() function to get the current time from user and depending on the value of that time you can update your greeting message by calling set state or using the corresponding method for your state management. You can apply if else conditions on the value obtained from DateTime.now() function.
Here’s some example code that can help you.
To add onto Naqeeb’s answer, the latest version of Flutter ships with Dart 3 which comes with switch expressions. That means the chained if-then blocks:
can be replaced with a pattern-powered switch block:
or better yet, a switch expression: