dialog_date.dart 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:get/get.dart';
  4. import 'alert_dialog.dart';
  5. import 'custom_date_picker.dart';
  6. class _VDialogDateValueCache {
  7. DateTime value;
  8. _VDialogDateValueCache(this.value);
  9. }
  10. /// 弹窗日期选择器
  11. class VDialogDate extends StatelessWidget {
  12. /// 标题
  13. final String? title;
  14. /// 初始值
  15. final DateTime? initialValue;
  16. /// 最小时间
  17. final DateTime? minimumDate;
  18. /// 最大值
  19. final DateTime? maxValue;
  20. const VDialogDate({
  21. super.key,
  22. this.title,
  23. this.initialValue,
  24. this.minimumDate,
  25. this.maxValue,
  26. });
  27. Future<DateTime?> show<DateTime>() => VAlertDialog.showDialog<DateTime>(this);
  28. @override
  29. Widget build(BuildContext context) {
  30. DateTime initialDateTime = initialValue ?? DateTime.now();
  31. final valueCache = _VDialogDateValueCache(
  32. DateTime(
  33. initialDateTime.year,
  34. initialDateTime.month,
  35. initialDateTime.day,
  36. ),
  37. );
  38. return VAlertDialog(
  39. title: title,
  40. contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 16),
  41. content: _buildContent(context, valueCache),
  42. showCancel: true,
  43. onConfirm: () {
  44. Get.back(result: valueCache.value);
  45. },
  46. );
  47. }
  48. Widget _buildContent(BuildContext context, _VDialogDateValueCache cache) {
  49. // 不保留时间部分
  50. return Container(
  51. margin: const EdgeInsets.all(10),
  52. height: 216,
  53. // width: 460,
  54. child: Stack(
  55. children: [
  56. Positioned.fill(
  57. child: VCustomCupertinoDatePicker(
  58. initialDateTime: cache.value,
  59. minimumYear: 1900,
  60. maximumYear: 2100,
  61. minimumDate: minimumDate,
  62. maximumDate: maxValue,
  63. backgroundColor: CupertinoColors.white,
  64. onDateTimeChanged: (date) {
  65. cache.value = date;
  66. },
  67. ),
  68. ),
  69. Positioned(
  70. top: 72,
  71. left: 0,
  72. right: 0,
  73. child: Divider(
  74. thickness: 1,
  75. color: Colors.black.withOpacity(0.2),
  76. indent: 12,
  77. endIndent: 12,
  78. ),
  79. ),
  80. Positioned(
  81. top: 72 + 58,
  82. left: 0,
  83. right: 0,
  84. child: Divider(
  85. thickness: 1,
  86. color: Colors.black.withOpacity(0.2),
  87. indent: 12,
  88. endIndent: 12,
  89. ),
  90. ),
  91. ],
  92. ),
  93. );
  94. }
  95. }