Skip to content

Commit 36a2950

Browse files
committed
feat: Adding Cupertino demo. #1
1 parent cc7a1c4 commit 36a2950

File tree

2 files changed

+136
-4
lines changed

2 files changed

+136
-4
lines changed

lib/cupertino.dart

+134
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import 'package:flutter/cupertino.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:intl/intl.dart';
4+
5+
/// Cupertino example page.
6+
/// Showcases the usage of `DatePicker` and `TimePicker` to change date and time.
7+
class CupertinoExamplePage extends StatefulWidget {
8+
const CupertinoExamplePage({super.key});
9+
10+
@override
11+
State<CupertinoExamplePage> createState() => _CupertinoExamplePageState();
12+
}
13+
14+
class _CupertinoExamplePageState extends State<CupertinoExamplePage> {
15+
DateTime dateTime = DateTime.now();
16+
17+
// This function displays a CupertinoModalPopup with a reasonable fixed height
18+
// which hosts CupertinoDatePicker.
19+
void _showDialog(Widget child) {
20+
showCupertinoModalPopup<void>(
21+
context: context,
22+
builder: (BuildContext context) => Container(
23+
height: 216,
24+
padding: const EdgeInsets.only(top: 6.0),
25+
// The Bottom margin is provided to align the popup above the system
26+
// navigation bar.
27+
margin: EdgeInsets.only(
28+
bottom: MediaQuery.of(context).viewInsets.bottom,
29+
),
30+
// Provide a background color for the popup.
31+
color: CupertinoColors.systemBackground.resolveFrom(context),
32+
// Use a SafeArea widget to avoid system overlaps.
33+
child: SafeArea(
34+
top: false,
35+
child: child,
36+
),
37+
),
38+
);
39+
}
40+
41+
@override
42+
Widget build(BuildContext context) {
43+
return Container(
44+
child: Padding(
45+
padding: const EdgeInsets.only(right: 8.0, left: 8.0),
46+
child: Column(
47+
mainAxisAlignment: MainAxisAlignment.center,
48+
children: [
49+
Row(
50+
mainAxisAlignment: MainAxisAlignment.center,
51+
children: [
52+
Text(
53+
DateFormat('yyyy-MM-dd').format(dateTime),
54+
style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
55+
),
56+
Text(
57+
DateFormat(' kk:mm').format(dateTime),
58+
style: const TextStyle(fontSize: 30),
59+
),
60+
],
61+
),
62+
Padding(
63+
padding: const EdgeInsets.only(top: 16.0),
64+
child: Row(
65+
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
66+
children: [
67+
ElevatedButton(
68+
style: ElevatedButton.styleFrom(backgroundColor: Colors.purple.shade300),
69+
onPressed: () => _showDialog(
70+
CupertinoDatePicker(
71+
initialDateTime: dateTime,
72+
mode: CupertinoDatePickerMode.date,
73+
use24hFormat: true,
74+
// This shows day of week alongside day of month
75+
showDayOfWeek: true,
76+
// This is called when the user changes the date.
77+
onDateTimeChanged: (DateTime newDate) {
78+
setState(() => dateTime = newDate);
79+
},
80+
),
81+
),
82+
child: const Text(
83+
"Date",
84+
style: TextStyle(fontSize: 20),
85+
)),
86+
ElevatedButton(
87+
style: ElevatedButton.styleFrom(backgroundColor: Colors.purple.shade300),
88+
child: const Text(
89+
"Time",
90+
style: TextStyle(fontSize: 20),
91+
),
92+
onPressed: () => _showDialog(
93+
CupertinoDatePicker(
94+
initialDateTime: dateTime,
95+
mode: CupertinoDatePickerMode.time,
96+
use24hFormat: true,
97+
onDateTimeChanged: (DateTime newDate) {
98+
setState(() => dateTime = newDate);
99+
},
100+
),
101+
))
102+
],
103+
),
104+
),
105+
Padding(
106+
padding: const EdgeInsets.only(top: 16.0),
107+
child: Row(
108+
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
109+
children: [
110+
ElevatedButton(
111+
style: ElevatedButton.styleFrom(backgroundColor: Colors.green.shade400),
112+
onPressed: () => _showDialog(
113+
CupertinoDatePicker(
114+
initialDateTime: dateTime,
115+
mode: CupertinoDatePickerMode.dateAndTime,
116+
use24hFormat: true,
117+
onDateTimeChanged: (DateTime newDate) {
118+
setState(() => dateTime = newDate);
119+
},
120+
),
121+
),
122+
child: const Text(
123+
"DateTime",
124+
style: TextStyle(fontSize: 20),
125+
)),
126+
],
127+
),
128+
)
129+
],
130+
),
131+
),
132+
);
133+
}
134+
}

lib/main.dart

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'package:app/cupertino.dart';
12
import 'package:flutter/material.dart';
23

34
import 'material.dart';
@@ -30,10 +31,7 @@ class _HomePageState extends State<HomePage> {
3031
/// List of pages
3132
final List<Widget> _pages = <Widget>[
3233
const MaterialExamplePage(),
33-
const Text(
34-
'Cupertino widget',
35-
style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
36-
)
34+
const CupertinoExamplePage()
3735
];
3836

3937
/// Callback function that changes the index to show the selected page

0 commit comments

Comments
 (0)