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