Skip to content

Commit 10e8659

Browse files
committed
feat: Add datetime pickers separately. #1
1 parent fd80e37 commit 10e8659

File tree

5 files changed

+90
-4
lines changed

5 files changed

+90
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,5 @@ Now we're ready to rock 🎸!
206206

207207
## 2. `Material` Date/Time Pickers
208208

209+
210+

lib/main.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:flutter/material.dart';
22

3+
import 'material.dart';
4+
35
void main() => runApp(const App());
46

57
/// App class
@@ -27,10 +29,7 @@ class _HomePageState extends State<HomePage> {
2729

2830
/// List of pages
2931
final List<Widget> _pages = <Widget>[
30-
const Text(
31-
'Material widget',
32-
style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
33-
),
32+
const MaterialExamplePage(),
3433
const Text(
3534
'Inline widget',
3635
style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),

lib/material.dart

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:intl/intl.dart';
3+
4+
class MaterialExamplePage extends StatefulWidget {
5+
const MaterialExamplePage({super.key});
6+
7+
@override
8+
State<MaterialExamplePage> createState() => _MaterialExamplePageState();
9+
}
10+
11+
class _MaterialExamplePageState extends State<MaterialExamplePage> {
12+
DateTime dateTime = DateTime.now();
13+
14+
Future<DateTime?> pickDate() => showDatePicker(context: context, initialDate: dateTime, firstDate: DateTime(1900), lastDate: DateTime(2100));
15+
16+
Future<TimeOfDay?> pickTime() => showTimePicker(context: context, initialTime: TimeOfDay(hour: dateTime.hour, minute: dateTime.minute));
17+
18+
@override
19+
Widget build(BuildContext context) {
20+
return Container(
21+
child: Padding(
22+
padding: const EdgeInsets.only(right: 8.0, left: 8.0),
23+
child: Column(
24+
mainAxisAlignment: MainAxisAlignment.center,
25+
children: [
26+
Row(
27+
mainAxisAlignment: MainAxisAlignment.center,
28+
children: [
29+
Text(
30+
DateFormat('yyyy-MM-dd').format(dateTime),
31+
style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
32+
),
33+
Text(
34+
DateFormat(' kk:mm').format(dateTime),
35+
style: const TextStyle(fontSize: 30),
36+
),
37+
],
38+
),
39+
Padding(
40+
padding: const EdgeInsets.only(top: 16.0),
41+
child: Row(
42+
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
43+
children: [
44+
ElevatedButton(
45+
child: const Text("Date", style: TextStyle(fontSize: 20),),
46+
onPressed: () async {
47+
final newDate = await pickDate();
48+
if (newDate == null) return; // person pressed 'CANCEL'
49+
50+
// Update datetime object that's shown with new date
51+
final newDateTime = DateTime(newDate.year, newDate.month, newDate.day, dateTime.hour, dateTime.minute);
52+
setState(
53+
() => dateTime = newDateTime,
54+
);
55+
}),
56+
ElevatedButton(
57+
child: const Text("Time", style: TextStyle(fontSize: 20),),
58+
onPressed: () async {
59+
final newTime = await pickTime();
60+
if (newTime == null) return; // person pressed 'CANCEL'
61+
62+
// Update datetime object that's shown with new time
63+
final newDateTime = DateTime(dateTime.year, dateTime.month, dateTime.day, newTime.hour, newTime.minute);
64+
setState(
65+
() => dateTime = newDateTime,
66+
);
67+
})
68+
],
69+
),
70+
)
71+
],
72+
),
73+
),
74+
);
75+
}
76+
}

pubspec.lock

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ packages:
7575
description: flutter
7676
source: sdk
7777
version: "0.0.0"
78+
intl:
79+
dependency: "direct main"
80+
description:
81+
name: intl
82+
sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d"
83+
url: "https://pub.dev"
84+
source: hosted
85+
version: "0.18.1"
7886
js:
7987
dependency: transitive
8088
description:

pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ dependencies:
3535
# The following adds the Cupertino Icons font to your application.
3636
# Use with the CupertinoIcons class for iOS style icons.
3737
cupertino_icons: ^1.0.2
38+
intl: ^0.18.1
3839

3940
dev_dependencies:
4041
flutter_test:

0 commit comments

Comments
 (0)