forked from md-siam/widget_of_the_day
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate_picker.dart
62 lines (56 loc) · 1.57 KB
/
date_picker.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import 'package:flutter/material.dart';
class MyDatePicker extends StatefulWidget {
const MyDatePicker({Key? key}) : super(key: key);
@override
_MyDatePickerState createState() => _MyDatePickerState();
}
class _MyDatePickerState extends State<MyDatePicker> {
// create datetime variable
DateTime _dateTime = DateTime.now();
// show date picker method
void _showDatePicker() {
showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(2000),
lastDate: DateTime(2025),
).then((value) => {
setState(() {
_dateTime = value!;
})
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Date Picker'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
//display choosen date
Text(
_dateTime.toString(),
style: const TextStyle(fontSize: 20, color: Colors.black),
),
// button
MaterialButton(
// on pressed execute _showDatePicker() method
onPressed: _showDatePicker,
color: Colors.deepPurple[400],
child: const Padding(
padding: EdgeInsets.all(10.0),
child: Text(
"Choose Date",
style: TextStyle(fontSize: 24, color: Colors.white),
),
),
),
],
),
),
);
}
}