dialog_time.dart 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. const VDialogDateTime({
  20. super.key,
  21. this.title,
  22. this.initialValue,
  23. this.minimumDate,
  24. this.maxValue,
  25. });
  26. Future<DateTime?> show<DateTime>() => VAlertDialog.showDialog<DateTime>(this);
  27. @override
  28. Widget build(BuildContext context) {
  29. DateTime initialDateTime = initialValue ?? DateTime.now();
  30. final valueCache = _VDialogDateValueCache(
  31. DateTime(
  32. initialDateTime.year,
  33. initialDateTime.month,
  34. initialDateTime.day,
  35. initialDateTime.hour,
  36. initialDateTime.minute,
  37. ),
  38. );
  39. return VAlertDialog(
  40. title: title,
  41. contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 16),
  42. content: _buildContent(context, valueCache),
  43. showCancel: true,
  44. onConfirm: () {
  45. Get.back(result: valueCache.value);
  46. },
  47. );
  48. }
  49. Widget _buildContent(BuildContext context, _VDialogDateValueCache cache) {
  50. // 不保留时间部分
  51. return Container(
  52. margin: const EdgeInsets.all(10),
  53. height: 216,
  54. //width: 800,
  55. child: CupertinoDatePicker(
  56. onDateTimeChanged: (DateTime value) {
  57. cache.value = value;
  58. },
  59. minimumYear: 1900,
  60. maximumYear: 2100,
  61. minimumDate: minimumDate,
  62. maximumDate: maxValue,
  63. backgroundColor: CupertinoColors.white,
  64. initialDateTime: cache.value,
  65. use24hFormat: true,
  66. ),
  67. );
  68. }
  69. }