dialog_date.dart 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. const VDialogDate({
  17. super.key,
  18. this.title,
  19. this.initialValue,
  20. });
  21. Future<T?> show<T>() => VAlertDialog.showDialog<T>(this);
  22. @override
  23. Widget build(BuildContext context) {
  24. DateTime initialDateTime = initialValue ?? DateTime.now();
  25. final valueCache = _VDialogDateValueCache(
  26. DateTime(
  27. initialDateTime.year,
  28. initialDateTime.month,
  29. initialDateTime.day,
  30. ),
  31. );
  32. return VAlertDialog(
  33. title: title,
  34. contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 8),
  35. content: _buildContent(context, valueCache),
  36. showCancel: true,
  37. onConfirm: () {
  38. Get.back(result: valueCache.value);
  39. },
  40. );
  41. }
  42. Widget _buildContent(BuildContext context, _VDialogDateValueCache cache) {
  43. // 不保留时间部分
  44. return Container(
  45. margin: const EdgeInsets.all(10),
  46. height: 216,
  47. // width: 460,
  48. child: Stack(
  49. children: [
  50. Positioned.fill(
  51. child: VCustomCupertinoDatePicker(
  52. initialDateTime: cache.value,
  53. minimumYear: 1900,
  54. maximumYear: 2100,
  55. backgroundColor: CupertinoColors.white,
  56. onDateTimeChanged: (date) {
  57. cache.value = date;
  58. },
  59. ),
  60. ),
  61. Positioned(
  62. top: 72,
  63. left: 0,
  64. right: 0,
  65. child: Divider(
  66. thickness: 1,
  67. color: Colors.black.withOpacity(0.2),
  68. indent: 12,
  69. endIndent: 12,
  70. ),
  71. ),
  72. Positioned(
  73. top: 72 + 58,
  74. left: 0,
  75. right: 0,
  76. child: Divider(
  77. thickness: 1,
  78. color: Colors.black.withOpacity(0.2),
  79. indent: 12,
  80. endIndent: 12,
  81. ),
  82. ),
  83. ],
  84. ),
  85. );
  86. }
  87. }