Ingen beskrivning

notifcation_dialog.dart 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'package:flutter/material.dart';
  2. import 'package:intl/intl.dart';
  3. Future<TimeOfDay> _selectTime(BuildContext context,
  4. {@required DateTime initialDate}) {
  5. final now = DateTime.now();
  6. return showTimePicker(
  7. context: context,
  8. initialTime: TimeOfDay(hour: initialDate.hour, minute: initialDate.minute),
  9. );
  10. }
  11. Future<DateTime> _selectDateTime(BuildContext context,
  12. {@required DateTime initialDate}) {
  13. final now = DateTime.now();
  14. final newestDate = initialDate.isAfter(now) ? initialDate : now;
  15. return showDatePicker(
  16. context: context,
  17. initialDate: newestDate.add(Duration(seconds: 1)),
  18. firstDate: now,
  19. lastDate: DateTime(2100),
  20. );
  21. }
  22. Dialog showDateTimeDialog(
  23. BuildContext context, {
  24. @required ValueChanged<DateTime> onSelectedDate,
  25. @required DateTime initialDate,
  26. }) {
  27. final dialog = Dialog(
  28. child: DateTimeDialog(
  29. onSelectedDate: onSelectedDate, initialDate: initialDate),
  30. );
  31. showDialog(context: context, builder: (BuildContext context) => dialog);
  32. }
  33. class DateTimeDialog extends StatefulWidget {
  34. final ValueChanged<DateTime> onSelectedDate;
  35. final DateTime initialDate;
  36. const DateTimeDialog({
  37. @required this.onSelectedDate,
  38. @required this.initialDate,
  39. Key key,
  40. }) : super(key: key);
  41. @override
  42. _DateTimeDialogState createState() => _DateTimeDialogState();
  43. }
  44. class _DateTimeDialogState extends State<DateTimeDialog> {
  45. DateTime selectedDate;
  46. @override
  47. void initState() {
  48. super.initState();
  49. selectedDate = widget.initialDate;
  50. }
  51. @override
  52. Widget build(BuildContext context) => Padding(
  53. padding: const EdgeInsets.all(16),
  54. child: Column(
  55. mainAxisAlignment: MainAxisAlignment.center,
  56. mainAxisSize: MainAxisSize.min,
  57. children: <Widget>[
  58. Text(
  59. 'Select time',
  60. style: Theme.of(context).textTheme.title,
  61. ),
  62. const SizedBox(height: 16),
  63. Row(
  64. mainAxisAlignment: MainAxisAlignment.center,
  65. children: <Widget>[
  66. RaisedButton(
  67. child: Text(DateFormat('yyyy-MM-dd').format(selectedDate)),
  68. onPressed: () async {
  69. final date = await _selectDateTime(context,
  70. initialDate: selectedDate);
  71. if (date == null) return;
  72. setState(() {
  73. selectedDate = DateTime(
  74. date.year,
  75. date.month,
  76. date.day,
  77. selectedDate.hour,
  78. selectedDate.minute,
  79. );
  80. });
  81. widget.onSelectedDate(selectedDate);
  82. },
  83. ),
  84. const SizedBox(width: 8),
  85. RaisedButton(
  86. child: Text(DateFormat('HH:mm').format(selectedDate)),
  87. onPressed: () async {
  88. final time =
  89. await _selectTime(context, initialDate: selectedDate);
  90. if (time == null) return;
  91. setState(() {
  92. selectedDate = DateTime(
  93. selectedDate.year,
  94. selectedDate.month,
  95. selectedDate.day,
  96. time.hour,
  97. time.minute,
  98. );
  99. });
  100. widget.onSelectedDate(selectedDate);
  101. },
  102. ),
  103. ],
  104. ),
  105. const SizedBox(height: 16),
  106. OutlineButton(
  107. child: Text('Schedule!'),
  108. onPressed: () {
  109. Navigator.of(context).pop();
  110. },
  111. highlightColor: Colors.orange,
  112. ),
  113. ],
  114. ),
  115. );
  116. }