123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- import 'package:cs_resources/constants/color_constants.dart';
- import 'package:cs_resources/generated/assets.dart';
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:plugin_basic/basic_export.dart';
- import 'package:plugin_platform/engine/toast/toast_engine.dart';
- import 'package:shared/utils/date_time_utils.dart';
- import 'package:widgets/ext/ex_widget.dart';
- import 'package:widgets/my_load_image.dart';
- import 'package:widgets/my_text_view.dart';
- import 'package:widgets/picker/date_picker_util.dart';
- import 'package:widgets/widget_export.dart';
- class DateRangePickerDialog extends StatefulWidget {
- final void Function(String) confirmAction; // 回调确认的时间区间
- final VoidCallback? cancelAction;
- final String? dateRange; // 时间区间参数
- DateRangePickerDialog({
- required this.confirmAction,
- this.cancelAction,
- this.dateRange, // 接收时间区间
- });
- @override
- _DateRangePickerDialogState createState() => _DateRangePickerDialogState();
- }
- class _DateRangePickerDialogState extends State<DateRangePickerDialog> {
- DateTime? startDate;
- DateTime? endDate;
- @override
- void initState() {
- super.initState();
- // 如果 dateRange 不为空,则解析并设置开始和结束日期
- if (widget.dateRange != null && widget.dateRange!.isNotEmpty) {
- List<String> dates = widget.dateRange!.split(',');
- if (dates.length >= 2) {
- startDate = DateTime.parse(dates[0]);
- endDate = DateTime.parse(dates[dates.length - 1]);
- }
- }
- }
- @override
- Widget build(BuildContext context) {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.center,
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Container(
- width: double.infinity,
- decoration: const BoxDecoration(
- color: Colors.white,
- borderRadius: BorderRadius.all(Radius.circular(15)),
- ),
- child: Column(
- children: [
- MyTextView(
- "Select Date Range".tr,
- fontSize: 19,
- isFontMedium: true,
- textColor: ColorConstants.black,
- marginTop: 15,
- marginBottom: 15,
- ),
- InkWell(
- onTap: () {
- DatePickerUtil.showCupertinoDatePicker(
- selectedDateTime: startDate ?? DateTime.now(),
- mode: CupertinoDatePickerMode.date,
- onDateTimeChanged: (date) {
- setState(() {
- startDate = date;
- });
- },
- title: "Start Time".tr,
- );
- },
- child: Container(
- decoration: const BoxDecoration(
- color: ColorConstants.grayECECEC,
- borderRadius: BorderRadius.all(Radius.circular(5)),
- ),
- margin: const EdgeInsets.symmetric(horizontal: 15),
- padding: const EdgeInsets.all(15.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- MyTextView(
- startDate != null ? DateTimeUtils.formatDate(startDate, format: 'yyyy-MM-dd') : "Select Start Time",
- fontSize: 15,
- isFontRegular: true,
- textColor: ColorConstants.black33,
- ),
- const MyAssetImage(
- Assets.cptJobPickDateIcon,
- width: 20,
- height: 20,
- color: ColorConstants.secondaryAppColor,
- ),
- ],
- ),
- ),
- ),
- InkWell(
- onTap: () {
- DatePickerUtil.showCupertinoDatePicker(
- selectedDateTime: endDate ?? DateTime.now(),
- mode: CupertinoDatePickerMode.date,
- onDateTimeChanged: (date) {
- setState(() {
- endDate = date;
- });
- },
- title: "End Time".tr,
- );
- },
- child: Container(
- decoration: const BoxDecoration(
- color: ColorConstants.grayECECEC,
- borderRadius: BorderRadius.all(Radius.circular(5)),
- ),
- margin: const EdgeInsets.only(left: 15, right: 15, top: 15),
- padding: const EdgeInsets.all(15.0),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- MyTextView(
- endDate != null ? DateTimeUtils.formatDate(endDate, format: 'yyyy-MM-dd') : "Select End Time",
- fontSize: 15,
- isFontRegular: true,
- textColor: ColorConstants.black33,
- ),
- const MyAssetImage(
- Assets.cptJobPickDateIcon,
- width: 20,
- height: 20,
- color: ColorConstants.secondaryAppColor,
- ),
- ],
- ),
- ),
- ),
- Container(
- margin: const EdgeInsets.only(top: 25),
- color: const Color(0XFFCECECE),
- height: 0.5,
- ),
- Row(
- children: [
- Expanded(
- flex: 1,
- child: InkWell(
- onTap: () {
- onCancel();
- widget.cancelAction?.call();
- },
- child: MyTextView(
- "Cancel".tr,
- fontSize: 17.5,
- isFontMedium: true,
- textAlign: TextAlign.center,
- textColor: const Color(0XFF0085C4),
- cornerRadius: 3,
- borderWidth: 1,
- ),
- )),
- Container(
- color: const Color(0xff09141F).withOpacity(0.13),
- width: 0.5,
- ),
- Expanded(
- flex: 1,
- child: InkWell(
- onTap: () async {
- if (startDate != null && endDate != null) {
- if (endDate!.isBefore(startDate!.add(const Duration(hours: 6)))) {
- ToastEngine.show("End date must be greater than start date");
- } else {
- onCancel();
- // 确保开始日期和结束日期都已选择
- String dateRange = _getDateRange(startDate!, endDate!);
- widget.confirmAction(dateRange);
- }
- } else {
- ToastEngine.show("Select Date");
- }
- },
- child: MyTextView(
- "Confirm".tr,
- marginLeft: 10,
- fontSize: 17.5,
- isFontMedium: true,
- textAlign: TextAlign.center,
- textColor: const Color(0XFF0085C4),
- cornerRadius: 3,
- ),
- )),
- ],
- ).constrained(height: 46),
- ],
- ),
- ),
- ],
- ).constrained(width: 295);
- }
- String _getDateRange(DateTime start, DateTime end) {
- List<String> dateList = [];
- DateTime current = start;
- String endFormat = DateTimeUtils.formatDate(end, format: 'yyyy-MM-dd');
- // 逐日增加,直到达到结束日期,包括结束日期
- while (current.isBefore(end) || current.isAtSameMomentAs(end)) {
- dateList.add(DateTimeUtils.formatDate(current, format: 'yyyy-MM-dd'));
- current = current.add(const Duration(days: 1));
- }
- if (!dateList.contains(endFormat)) {
- dateList.add(endFormat);
- }
- return dateList.join(','); // 使用逗号连接日期
- }
- void onCancel() async {
- SmartDialog.dismiss();
- }
- }
|