@@ -496,4 +496,206 @@ you should see the changes!
496
496
</p >
497
497
498
498
499
- ## 3. (Option 2) ` Cupertino ` Pickers
499
+ ## 3. (Option 2) ` Cupertino ` Pickers
500
+
501
+ While you may not want to use ` Material ` widgets,
502
+ ` Flutter ` also offers official support for
503
+ [ ** ` Cupertino ` ** widgets ] ( https://docs.flutter.dev/ui/widgets/cupertino ) ,
504
+ which are iOS-style widgets.
505
+
506
+ Let's implement them!
507
+ First, let's create a new page ` lib/cupertino.dart `
508
+ and * use it* in ` lib/main.dart ` .
509
+
510
+ ``` dart
511
+ final List<Widget> _pages = <Widget>[
512
+ const MaterialExamplePage(),
513
+ const CupertinoExamplePage()
514
+ ];
515
+ ```
516
+
517
+ Now, in ` lib/cupertino.dart ` , add the following code.
518
+
519
+ ``` dart
520
+ import 'package:flutter/cupertino.dart';
521
+ import 'package:flutter/material.dart';
522
+ import 'package:intl/intl.dart';
523
+
524
+ /// Cupertino example page.
525
+ /// Showcases the usage of `DatePicker` and `TimePicker` to change date and time.
526
+ class CupertinoExamplePage extends StatefulWidget {
527
+ const CupertinoExamplePage({super.key});
528
+
529
+ @override
530
+ State<CupertinoExamplePage> createState() => _CupertinoExamplePageState();
531
+ }
532
+
533
+ class _CupertinoExamplePageState extends State<CupertinoExamplePage> {
534
+ DateTime dateTime = DateTime.now();
535
+
536
+ // This function displays a CupertinoModalPopup with a reasonable fixed height
537
+ // which hosts CupertinoDatePicker.
538
+ void _showDialog(Widget child) {
539
+ showCupertinoModalPopup<void>(
540
+ context: context,
541
+ builder: (BuildContext context) => Container(
542
+ height: 216,
543
+ padding: const EdgeInsets.only(top: 6.0),
544
+ // The Bottom margin is provided to align the popup above the system
545
+ // navigation bar.
546
+ margin: EdgeInsets.only(
547
+ bottom: MediaQuery.of(context).viewInsets.bottom,
548
+ ),
549
+ // Provide a background color for the popup.
550
+ color: CupertinoColors.systemBackground.resolveFrom(context),
551
+ // Use a SafeArea widget to avoid system overlaps.
552
+ child: SafeArea(
553
+ top: false,
554
+ child: child,
555
+ ),
556
+ ),
557
+ );
558
+ }
559
+
560
+ @override
561
+ Widget build(BuildContext context) {
562
+ return Container(
563
+ child: Padding(
564
+ padding: const EdgeInsets.only(right: 8.0, left: 8.0),
565
+ child: Column(
566
+ mainAxisAlignment: MainAxisAlignment.center,
567
+ children: [
568
+ Row(
569
+ mainAxisAlignment: MainAxisAlignment.center,
570
+ children: [
571
+ Text(
572
+ DateFormat('yyyy-MM-dd').format(dateTime),
573
+ style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
574
+ ),
575
+ Text(
576
+ DateFormat(' kk:mm').format(dateTime),
577
+ style: const TextStyle(fontSize: 30),
578
+ ),
579
+ ],
580
+ ),
581
+ Padding(
582
+ padding: const EdgeInsets.only(top: 16.0),
583
+ child: Row(
584
+ mainAxisAlignment: MainAxisAlignment.spaceEvenly,
585
+ children: [
586
+ ElevatedButton(
587
+ style: ElevatedButton.styleFrom(backgroundColor: Colors.purple.shade300),
588
+ onPressed: () => _showDialog(
589
+ CupertinoDatePicker(
590
+ initialDateTime: dateTime,
591
+ mode: CupertinoDatePickerMode.date,
592
+ use24hFormat: true,
593
+ // This shows day of week alongside day of month
594
+ showDayOfWeek: true,
595
+ // This is called when the user changes the date.
596
+ onDateTimeChanged: (DateTime newDate) {
597
+ setState(() => dateTime = newDate);
598
+ },
599
+ ),
600
+ ),
601
+ child: const Text(
602
+ "Date",
603
+ style: TextStyle(fontSize: 20),
604
+ )),
605
+ ElevatedButton(
606
+ style: ElevatedButton.styleFrom(backgroundColor: Colors.purple.shade300),
607
+ child: const Text(
608
+ "Time",
609
+ style: TextStyle(fontSize: 20),
610
+ ),
611
+ onPressed: () => _showDialog(
612
+ CupertinoDatePicker(
613
+ initialDateTime: dateTime,
614
+ mode: CupertinoDatePickerMode.time,
615
+ use24hFormat: true,
616
+ onDateTimeChanged: (DateTime newDate) {
617
+ setState(() => dateTime = newDate);
618
+ },
619
+ ),
620
+ ))
621
+ ],
622
+ ),
623
+ ),
624
+ Padding(
625
+ padding: const EdgeInsets.only(top: 16.0),
626
+ child: Row(
627
+ mainAxisAlignment: MainAxisAlignment.spaceEvenly,
628
+ children: [
629
+ ElevatedButton(
630
+ style: ElevatedButton.styleFrom(backgroundColor: Colors.green.shade400),
631
+ onPressed: () => _showDialog(
632
+ CupertinoDatePicker(
633
+ initialDateTime: dateTime,
634
+ mode: CupertinoDatePickerMode.dateAndTime,
635
+ use24hFormat: true,
636
+ onDateTimeChanged: (DateTime newDate) {
637
+ setState(() => dateTime = newDate);
638
+ },
639
+ ),
640
+ ),
641
+ child: const Text(
642
+ "DateTime",
643
+ style: TextStyle(fontSize: 20),
644
+ )),
645
+ ],
646
+ ),
647
+ )
648
+ ],
649
+ ),
650
+ ),
651
+ );
652
+ }
653
+ }
654
+ ```
655
+
656
+ Let's break it down!
657
+ ` Cupertino ` widgets only work with the
658
+ [ ` showCupertinoModalPopup ` ] ( https://api.flutter.dev/flutter/cupertino/showCupertinoModalPopup.html )
659
+ function.
660
+ This is why we create the ` _showDialog ` function
661
+ which * builds* the popup modal.
662
+ This function will be used in each button to change the ` dateTime ` object,
663
+ similarly to the ones found in ` lib/material.dart ` .
664
+
665
+ In each button,
666
+ when pressed,
667
+ we mutate the ` dateTime ` field in a similar fashion.
668
+ For example, check the following snippet:
669
+
670
+ ``` dart
671
+ _showDialog(
672
+ CupertinoDatePicker(
673
+ initialDateTime: dateTime,
674
+ mode: CupertinoDatePickerMode.date,
675
+ use24hFormat: true,
676
+ // This shows day of week alongside day of month
677
+ showDayOfWeek: true,
678
+ // This is called when the user changes the date.
679
+ onDateTimeChanged: (DateTime newDate) {
680
+ setState(() => dateTime = newDate);
681
+ },
682
+ ),
683
+ )
684
+ ```
685
+
686
+ This is invoked when the person wants to change the ** date** .
687
+ The ` mode ` parameter of [ ` CupertinoDatePicker ` ] ( https://api.flutter.dev/flutter/cupertino/CupertinoDatePicker-class.html )
688
+ will define a different behaviour.
689
+ If we were to change the time, we'd do ` CupertinoDatePickerMode.time ` .
690
+ Similarly, in the "DateTime" button,
691
+ we change to ` CupertinoDatePickerMode.dateAndTime ` .
692
+
693
+ And that's it!
694
+ If we run,
695
+ we can change the ` dateTime ` object accordingly!
696
+
697
+
698
+ <p align =' center ' >
699
+ <img width="250" src="https://github.com/dwyl/flutter-date-time-tutorial/assets/17494745/4d7fdf39-ed7b-40fa-a7f2-5fbd794ccdfc">
700
+ </p >
701
+
0 commit comments