'How to add Good Morning Salutation like Spotify in flutter application
I want to make salutation based on the timings in my app in flutter just like spotify -
Also i am having doubt regarding where to place this piece of code and if i paste it anywhere in main.dart. I get an error. The code-
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';
}
}
Solution 1:[1]
You need to create a Widget that displays the text of your greeting. The Widget must also be in your build method to be displayed. Below is an example of a Text Widget displaying the greeting at the center of the screen:
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('My Flutter app'),
),
body: Center(
child: Text(_getGreeting()),
),
),
);
}
String _getGreeting() {
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';
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

