DatePicker and TimePicker

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",
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blueGrey,
),
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) {
return Scaffold(
appBar: AppBar(
title: const Text(
"Hello Flutter Application",
style: TextStyle(fontWeight: FontWeight.bold),
),
centerTitle: true,
backgroundColor: Colors.grey,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text("Select Date",
style: TextStyle(
fontSize: 25,
)),
ElevatedButton(
onPressed: () async {
DateTime? datePicked = await showDatePicker(
/*Here, return type is "DateTime". DateTime reference will get. This process will handle asynchronously.
because when DateTimePicker open and then we select the date.This process may take several minutes(long time).
and we got delay for our main process.Main process may be stop.hence we have to use await so that we can handle
this process asynchronously.so that this will wait until the result will throw.we can also use Future class
for the same."DateTime?" denotes that it will expect null value also.so we have to use "?" after DateTime.when the
user pick the date then that date will come in "datePicked" variable. */

context: context,
//firstDate: DateTime(DateTime.now().year-5), //This pattern also Runnable(Executable)
//lastDate: DateTime(DateTime.now().year+5), //initialDate must be on or after firstDate
//initialDate: DateTime.now(),

/*initialDate: DateTime(2001,2), //initialDate must be on or after firstDate
firstDate: DateTime(2001,1), //This pattern also Runnable(Executable)
lastDate: DateTime(2023),*/

initialDate: DateTime(2001, 3),
//initialDate must be on or after firstDate
firstDate: DateTime(2001, 3),
//This pattern also Runnable(Executable)
lastDate: DateTime(2024),
);

if (datePicked != null) {
print(
"Date selected: ${datePicked.day}-${datePicked.month}-${datePicked.year}");
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
fixedSize: const Size(350.0, 50.0),
),
child: const Text(
"Show current Time",
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.white),
),
),
const SizedBox(height: 50.0),
/*we can use "Future" function also instead of await async.*/
ElevatedButton(
onPressed: () async {
TimeOfDay? pickedTime = await showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
initialEntryMode: TimePickerEntryMode
.dial, //By default, "dial" mode will open everytime executing
//initialEntryMode: TimePickerEntryMode.input, //Now, "input" mode will open while executing
);

if (pickedTime != null) {
print(
"Selected Time: ${pickedTime.hour} : ${pickedTime.minute}");
}
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
fixedSize: const Size(350.0, 50.0),
),
child: const Text(
"Select 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