123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
- import 'package:get/get.dart';
- import 'package:cs_resources/constants/color_constants.dart';
- import 'package:shared/utils/date_time_utils.dart';
- import '../utils/dark_theme_util.dart';
- class DatePickerUtil {
- static void showCupertinoDatePicker({
- required Function(DateTime) onDateTimeChanged,
- DateTime? selectedDateTime,
- int minuteInterval = 1,
- DateTime? minimumDate,
- DateTime? maximumDate,
- CupertinoDatePickerMode mode = CupertinoDatePickerMode.date,
- String title = '',
- DatePickerDateOrder? dateOrder,
- }) {
- int curMillisecond = DateTime.now().millisecondsSinceEpoch; //毫秒
- selectedDateTime ??= DateTimeUtils.getDateTimeByMs(curMillisecond); //当前选中的日期
- minimumDate ??= DateTimeUtils.getDateTimeByMs(curMillisecond - 10 * 365 * 24 * 60 * 60 * 1000); //最小范围默认减去10年
- maximumDate ??= DateTimeUtils.getDateTimeByMs(curMillisecond + 10 * 365 * 24 * 60 * 60 * 1000); //最大范围默认加上10年
- SmartDialog.show(
- usePenetrate: false,
- tag: 'date_picker',
- maskColor: Colors.black.withOpacity(0.4),
- alignment: Alignment.bottomCenter,
- builder: (BuildContext context) {
- return Container(
- height: 280,
- decoration: BoxDecoration(
- color: DarkThemeUtil.multiColors(Colors.white, darkColor: ColorConstants.darkBlackItem),
- borderRadius: const BorderRadius.only(topLeft: Radius.circular(25), topRight: Radius.circular(25)),
- ),
- child: Column(
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- CupertinoButton(
- child: Text(
- 'Cancel'.tr,
- style: TextStyle(
- fontSize: 17,
- fontWeight: FontWeight.w500,
- color: ColorConstants.appBlue,
- ),
- ),
- onPressed: () => SmartDialog.dismiss(tag: 'date_picker'),
- ),
- Text(
- title.tr,
- style: TextStyle(
- fontSize: 18,
- fontWeight: FontWeight.w500,
- color: DarkThemeUtil.multiColors(const Color(0xff404A5B), darkColor: Colors.white),
- ),
- ),
- CupertinoButton(
- child: Text(
- 'Confirm'.tr,
- style: TextStyle(
- fontSize: 17,
- fontWeight: FontWeight.w500,
- color: ColorConstants.appBlue,
- ),
- ),
- onPressed: () {
- SmartDialog.dismiss(tag: 'date_picker');
- // 将选中的值通过回调函数传递出去
- onDateTimeChanged(selectedDateTime!);
- },
- ),
- ],
- ),
- Divider(
- color: DarkThemeUtil.multiColors(
- ColorConstants.dividerA6,
- darkColor: ColorConstants.darkBlackItemDivider,
- ),
- height: 0.5),
- Expanded(
- child: CupertinoDatePicker(
- mode: mode,
- use24hFormat: true,
- minimumDate: minimumDate,
- maximumDate: maximumDate,
- initialDateTime: selectedDateTime,
- onDateTimeChanged: (date) {
- selectedDateTime = date;
- },
- dateOrder: dateOrder,
- ),
- ),
- ],
- ),
- );
- },
- );
- }
- }
|