date_picker_util.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import 'package:flutter/cupertino.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
  4. import 'package:get/get.dart';
  5. import 'package:cs_resources/constants/color_constants.dart';
  6. import 'package:shared/utils/date_time_utils.dart';
  7. import '../utils/dark_theme_util.dart';
  8. class DatePickerUtil {
  9. static void showCupertinoDatePicker({
  10. required Function(DateTime) onDateTimeChanged,
  11. DateTime? selectedDateTime,
  12. int minuteInterval = 1,
  13. DateTime? minimumDate,
  14. DateTime? maximumDate,
  15. CupertinoDatePickerMode mode = CupertinoDatePickerMode.date,
  16. String title = '',
  17. DatePickerDateOrder? dateOrder,
  18. }) {
  19. int curMillisecond = DateTime.now().millisecondsSinceEpoch; //毫秒
  20. selectedDateTime ??= DateTimeUtils.getDateTimeByMs(curMillisecond); //当前选中的日期
  21. minimumDate ??= DateTimeUtils.getDateTimeByMs(curMillisecond - 10 * 365 * 24 * 60 * 60 * 1000); //最小范围默认减去10年
  22. maximumDate ??= DateTimeUtils.getDateTimeByMs(curMillisecond + 10 * 365 * 24 * 60 * 60 * 1000); //最大范围默认加上10年
  23. SmartDialog.show(
  24. usePenetrate: false,
  25. tag: 'date_picker',
  26. maskColor: Colors.black.withOpacity(0.4),
  27. alignment: Alignment.bottomCenter,
  28. builder: (BuildContext context) {
  29. return Container(
  30. height: 280,
  31. decoration: BoxDecoration(
  32. color: DarkThemeUtil.multiColors(Colors.white, darkColor: ColorConstants.darkBlackItem),
  33. borderRadius: const BorderRadius.only(topLeft: Radius.circular(25), topRight: Radius.circular(25)),
  34. ),
  35. child: Column(
  36. children: [
  37. Row(
  38. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  39. children: [
  40. CupertinoButton(
  41. child: Text(
  42. 'Cancel'.tr,
  43. style: TextStyle(
  44. fontSize: 17,
  45. fontWeight: FontWeight.w500,
  46. color: ColorConstants.appBlue,
  47. ),
  48. ),
  49. onPressed: () => SmartDialog.dismiss(tag: 'date_picker'),
  50. ),
  51. Text(
  52. title.tr,
  53. style: TextStyle(
  54. fontSize: 18,
  55. fontWeight: FontWeight.w500,
  56. color: DarkThemeUtil.multiColors(const Color(0xff404A5B), darkColor: Colors.white),
  57. ),
  58. ),
  59. CupertinoButton(
  60. child: Text(
  61. 'Confirm'.tr,
  62. style: TextStyle(
  63. fontSize: 17,
  64. fontWeight: FontWeight.w500,
  65. color: ColorConstants.appBlue,
  66. ),
  67. ),
  68. onPressed: () {
  69. SmartDialog.dismiss(tag: 'date_picker');
  70. // 将选中的值通过回调函数传递出去
  71. onDateTimeChanged(selectedDateTime!);
  72. },
  73. ),
  74. ],
  75. ),
  76. Divider(
  77. color: DarkThemeUtil.multiColors(
  78. ColorConstants.dividerA6,
  79. darkColor: ColorConstants.darkBlackItemDivider,
  80. ),
  81. height: 0.5),
  82. Expanded(
  83. child: CupertinoDatePicker(
  84. mode: mode,
  85. use24hFormat: true,
  86. minimumDate: minimumDate,
  87. maximumDate: maximumDate,
  88. initialDateTime: selectedDateTime,
  89. onDateTimeChanged: (date) {
  90. selectedDateTime = date;
  91. },
  92. dateOrder: dateOrder,
  93. ),
  94. ),
  95. ],
  96. ),
  97. );
  98. },
  99. );
  100. }
  101. }