date_range_picker_dialog.dart 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import 'package:cs_resources/constants/color_constants.dart';
  2. import 'package:cs_resources/generated/assets.dart';
  3. import 'package:flutter/cupertino.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:plugin_basic/basic_export.dart';
  6. import 'package:plugin_platform/engine/toast/toast_engine.dart';
  7. import 'package:shared/utils/date_time_utils.dart';
  8. import 'package:widgets/ext/ex_widget.dart';
  9. import 'package:widgets/my_load_image.dart';
  10. import 'package:widgets/my_text_view.dart';
  11. import 'package:widgets/picker/date_picker_util.dart';
  12. import 'package:widgets/widget_export.dart';
  13. class DateRangePickerDialog extends StatefulWidget {
  14. final void Function(String) confirmAction; // 回调确认的时间区间
  15. final VoidCallback? cancelAction;
  16. DateRangePickerDialog({
  17. required this.confirmAction,
  18. this.cancelAction,
  19. });
  20. @override
  21. _DateRangePickerDialogState createState() => _DateRangePickerDialogState();
  22. }
  23. class _DateRangePickerDialogState extends State<DateRangePickerDialog> {
  24. DateTime? startDate;
  25. DateTime? endDate;
  26. @override
  27. Widget build(BuildContext context) {
  28. return Column(
  29. crossAxisAlignment: CrossAxisAlignment.center,
  30. mainAxisAlignment: MainAxisAlignment.center,
  31. children: [
  32. Container(
  33. width: double.infinity,
  34. decoration: const BoxDecoration(
  35. color: Colors.white,
  36. borderRadius: BorderRadius.all(Radius.circular(15)),
  37. ),
  38. child: Column(
  39. children: [
  40. MyTextView(
  41. "Select Date Range".tr,
  42. fontSize: 19,
  43. isFontMedium: true,
  44. textColor: ColorConstants.black,
  45. marginTop: 15,
  46. marginBottom: 15,
  47. ),
  48. InkWell(
  49. onTap: () {
  50. DatePickerUtil.showCupertinoDatePicker(
  51. selectedDateTime: startDate ?? DateTime.now(),
  52. mode: CupertinoDatePickerMode.date,
  53. onDateTimeChanged: (date) {
  54. setState(() {
  55. startDate = date;
  56. });
  57. },
  58. title: "Start Time".tr,
  59. );
  60. },
  61. child: Container(
  62. decoration: const BoxDecoration(
  63. color: ColorConstants.grayECECEC,
  64. borderRadius: BorderRadius.all(Radius.circular(5)),
  65. ),
  66. margin: const EdgeInsets.symmetric(horizontal: 15),
  67. padding: const EdgeInsets.all(15.0),
  68. child: Row(
  69. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  70. children: [
  71. MyTextView(
  72. startDate != null ? DateTimeUtils.formatDate(startDate, format: 'yyyy-MM-dd') : "Select Start Time",
  73. fontSize: 15,
  74. isFontRegular: true,
  75. textColor: ColorConstants.black33,
  76. ),
  77. const MyAssetImage(
  78. Assets.cptJobPickDateIcon,
  79. width: 20,
  80. height: 20,
  81. color: ColorConstants.secondaryAppColor,
  82. ),
  83. ],
  84. ),
  85. ),
  86. ),
  87. InkWell(
  88. onTap: () {
  89. DatePickerUtil.showCupertinoDatePicker(
  90. selectedDateTime: endDate ?? DateTime.now(),
  91. mode: CupertinoDatePickerMode.date,
  92. onDateTimeChanged: (date) {
  93. setState(() {
  94. endDate = date;
  95. });
  96. },
  97. title: "End Time".tr,
  98. );
  99. },
  100. child: Container(
  101. decoration: const BoxDecoration(
  102. color: ColorConstants.grayECECEC,
  103. borderRadius: BorderRadius.all(Radius.circular(5)),
  104. ),
  105. margin: const EdgeInsets.only(left: 15, right: 15, top: 15),
  106. padding: const EdgeInsets.all(15.0),
  107. child: Row(
  108. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  109. children: [
  110. MyTextView(
  111. endDate != null ? DateTimeUtils.formatDate(endDate, format: 'yyyy-MM-dd') : "Select End Time",
  112. fontSize: 15,
  113. isFontRegular: true,
  114. textColor: ColorConstants.black33,
  115. ),
  116. const MyAssetImage(
  117. Assets.cptJobPickDateIcon,
  118. width: 20,
  119. height: 20,
  120. color: ColorConstants.secondaryAppColor,
  121. ),
  122. ],
  123. ),
  124. ),
  125. ),
  126. Container(
  127. margin: const EdgeInsets.only(top: 25),
  128. color: const Color(0XFFCECECE),
  129. height: 0.5,
  130. ),
  131. Row(
  132. children: [
  133. Expanded(
  134. flex: 1,
  135. child: InkWell(
  136. onTap: () {
  137. onCancel();
  138. widget.cancelAction?.call();
  139. },
  140. child: MyTextView(
  141. "Cancel".tr,
  142. fontSize: 17.5,
  143. isFontMedium: true,
  144. textAlign: TextAlign.center,
  145. textColor: const Color(0XFF0085C4),
  146. cornerRadius: 3,
  147. borderWidth: 1,
  148. ),
  149. )),
  150. Container(
  151. color: const Color(0xff09141F).withOpacity(0.13),
  152. width: 0.5,
  153. ),
  154. Expanded(
  155. flex: 1,
  156. child: InkWell(
  157. onTap: () async {
  158. if (startDate != null && endDate != null) {
  159. if (endDate!.isBefore(startDate!.add(const Duration(hours: 6)))) {
  160. ToastEngine.show("End date must be greater than start date");
  161. }else{
  162. onCancel();
  163. // 确保开始日期和结束日期都已选择
  164. String dateRange = _getDateRange(startDate!, endDate!);
  165. widget.confirmAction(dateRange);
  166. }
  167. } else {
  168. ToastEngine.show("Select Date");
  169. }
  170. },
  171. child: MyTextView(
  172. "Confirm".tr,
  173. marginLeft: 10,
  174. fontSize: 17.5,
  175. isFontMedium: true,
  176. textAlign: TextAlign.center,
  177. textColor: const Color(0XFF0085C4),
  178. cornerRadius: 3,
  179. ),
  180. )),
  181. ],
  182. ).constrained(height: 46),
  183. ],
  184. ),
  185. ),
  186. ],
  187. ).constrained(width: 295);
  188. }
  189. String _getDateRange(DateTime start, DateTime end) {
  190. List<String> dateList = [];
  191. DateTime current = start;
  192. String endFormat = DateTimeUtils.formatDate(end, format: 'yyyy-MM-dd');
  193. // 逐日增加,直到达到结束日期,包括结束日期
  194. while (current.isBefore(end) || current.isAtSameMomentAs(end)) {
  195. dateList.add(DateTimeUtils.formatDate(current, format: 'yyyy-MM-dd'));
  196. current = current.add(const Duration(days: 1));
  197. }
  198. if (!dateList.contains(endFormat)) {
  199. dateList.add(endFormat);
  200. }
  201. return dateList.join(','); // 使用逗号连接日期
  202. }
  203. void onCancel() async {
  204. SmartDialog.dismiss();
  205. }
  206. }