|
| 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 | +} |
0 commit comments