dialog_time.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:get/get.dart';
  4. import 'alert_dialog.dart';
  5. class _VDialogDateValueCache {
  6. DateTime value;
  7. _VDialogDateValueCache(this.value);
  8. }
  9. /// 弹窗日期选择器
  10. class VDialogDateTime extends StatelessWidget {
  11. /// 标题
  12. final String? title;
  13. /// 初始值
  14. final DateTime? initialValue;
  15. /// 最小时间
  16. final DateTime? minimumDate;
  17. /// 最大值
  18. final DateTime? maxValue;
  19. final CupertinoDatePickerMode? mode;
  20. const VDialogDateTime({
  21. super.key,
  22. this.title,
  23. this.initialValue,
  24. this.minimumDate,
  25. this.maxValue,
  26. this.mode,
  27. });
  28. Future<DateTime?> show<DateTime>() => VAlertDialog.showDialog<DateTime>(this);
  29. @override
  30. Widget build(BuildContext context) {
  31. DateTime initialDateTime = initialValue ?? DateTime.now();
  32. final valueCache = _VDialogDateValueCache(
  33. DateTime(
  34. initialDateTime.year,
  35. initialDateTime.month,
  36. initialDateTime.day,
  37. initialDateTime.hour,
  38. initialDateTime.minute,
  39. ),
  40. );
  41. return VAlertDialog(
  42. title: title,
  43. contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 16),
  44. content: _buildContent(context, valueCache),
  45. showCancel: true,
  46. onConfirm: () {
  47. Get.back(result: valueCache.value);
  48. },
  49. );
  50. }
  51. Widget _buildContent(BuildContext context, _VDialogDateValueCache cache) {
  52. // 不保留时间部分
  53. return Container(
  54. margin: const EdgeInsets.all(10),
  55. height: 216,
  56. //width: 800,
  57. child: CupertinoDatePicker(
  58. onDateTimeChanged: (DateTime value) {
  59. cache.value = value;
  60. },
  61. minimumYear: 1900,
  62. maximumYear: 2100,
  63. minimumDate: minimumDate,
  64. maximumDate: maxValue,
  65. backgroundColor: CupertinoColors.white,
  66. initialDateTime: cache.value,
  67. use24hFormat: true,
  68. mode: mode ?? CupertinoDatePickerMode.dateAndTime,
  69. ),
  70. );
  71. }
  72. }
  73. /// 弹窗日期选择器
  74. class VDialogDateTime2 extends StatelessWidget {
  75. /// 标题
  76. final String? title;
  77. /// 初始值
  78. final DateTime? initialValue;
  79. /// 最小时间
  80. final DateTime? minimumDate;
  81. /// 最大值
  82. final DateTime? maxValue;
  83. const VDialogDateTime2({
  84. super.key,
  85. this.title,
  86. this.initialValue,
  87. this.minimumDate,
  88. this.maxValue,
  89. });
  90. Future<DateTime?> show<DateTime>() => VAlertDialog.showDialog<DateTime>(this);
  91. @override
  92. Widget build(BuildContext context) {
  93. DateTime initialDateTime = initialValue ?? DateTime.now();
  94. final valueCache = _VDialogDateValueCache(
  95. DateTime(
  96. initialDateTime.year,
  97. initialDateTime.month,
  98. initialDateTime.day,
  99. initialDateTime.hour,
  100. initialDateTime.minute,
  101. ),
  102. );
  103. return VAlertDialog(
  104. title: title,
  105. contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 16),
  106. content: _buildContent(context, valueCache),
  107. showCancel: true,
  108. onConfirm: () {
  109. Get.back(result: valueCache.value);
  110. },
  111. );
  112. }
  113. Widget _buildContent(BuildContext context, _VDialogDateValueCache cache) {
  114. // 不保留时间部分
  115. return Container(
  116. margin: const EdgeInsets.all(10),
  117. height: 216,
  118. //width: 800,
  119. child: CupertinoDatePicker(
  120. onDateTimeChanged: (DateTime value) {
  121. cache.value = value;
  122. },
  123. mode: CupertinoDatePickerMode.time,
  124. minimumYear: 1900,
  125. maximumYear: 2100,
  126. minimumDate: minimumDate,
  127. maximumDate: maxValue,
  128. backgroundColor: CupertinoColors.white,
  129. initialDateTime: cache.value,
  130. use24hFormat: true,
  131. ),
  132. );
  133. }
  134. }