Current Data and Time

 import "package:flutter/material.dart";


void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return MaterialApp(
title: "Hello Flutter",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.green,
),
home: const FlutterApp(),
);
}
}

class FlutterApp extends StatefulWidget {
const FlutterApp({super.key});

@override
State<StatefulWidget> createState() {
return FlutterState();
}
}

class FlutterState extends State<FlutterApp> {
@override
Widget build(BuildContext context) {
var time = DateTime.now();
/*DateTime.now() gives current Time.when build function called,
at that time, Exact(current) time will get in "time" variable. There will be stored the reference
of DateTime class. Here "now()" function is the static function So there is no need to call the object of class.
only class-reference is required. DateTime class will get all static components.*/

return Scaffold(
appBar: AppBar(
title: const Text(
"Welcome To Flutter Application",
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: Container(
//width: 200,
//height: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
"Current Time: $time",
style: const TextStyle(fontSize: 25),
),
//Showing current Time
const SizedBox(
height: 10,
),

Text(
"Current Year: ${time.year}",
style: const TextStyle(fontSize: 25),
),
//Showing current Year
const SizedBox(
height: 10,
),

Text(
"Current Month: ${time.month}",
style: const TextStyle(fontSize: 25),
),
//Showing current Month
const SizedBox(
height: 10,
),

Text(
"Current Day: ${time.day}",
style: const TextStyle(fontSize: 25),
),
//Showing current Day
const SizedBox(
height: 10,
),

Text(
"Current Weekday: ${time.weekday}",
style: const TextStyle(fontSize: 25),
),
//Showing current weekDay
const SizedBox(
height: 10,
),

Text(
"Current clockTime: ${time.hour}:${time.minute}:${time.second}",
style: const TextStyle(
fontSize: 25,
),
),
const SizedBox(
height: 50,
),
//Showing clockTime -> Hour:Minute:Second

ElevatedButton(
onPressed: () {
setState(() {
/*setState() function build the state again.when the elevatedButton pressed, setState() function
will call and build new state. At that time, above "build()" function will call.After any change, to refresh
UI setState() function will help.when button pressed, the new current value will update in variable "time".*/
});
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
fixedSize: const Size(350.0, 50.0),
),
child: const Text(
"Current Time",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white),
),
),
],
),
),
),
);
}
}

Comments

Popular posts from this blog

Pagination with Bloc Pattern in Flutter

Pagination First Practical in Flutter

ExpansionPanel with ExpansionPanelList with Complete Collapse Operation in Flutter