123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import 'package:flutter/cupertino.dart';
- import 'package:flutter/foundation.dart';
- import 'package:get/get.dart';
- import 'alert_dialog.dart';
- class _VDialogDateValueCache {
- DateTime value;
- _VDialogDateValueCache(this.value);
- }
- /// 弹窗日期选择器
- class VDialogDateTime extends StatelessWidget {
- /// 标题
- final String? title;
- /// 初始值
- final DateTime? initialValue;
- /// 最小时间
- final DateTime? minimumDate;
- /// 最大值
- final DateTime? maxValue;
- const VDialogDateTime({
- super.key,
- this.title,
- this.initialValue,
- this.minimumDate,
- this.maxValue,
- });
- Future<DateTime?> show<DateTime>() => VAlertDialog.showDialog<DateTime>(this);
- @override
- Widget build(BuildContext context) {
- DateTime initialDateTime = initialValue ?? DateTime.now();
- final valueCache = _VDialogDateValueCache(
- DateTime(
- initialDateTime.year,
- initialDateTime.month,
- initialDateTime.day,
- initialDateTime.hour,
- initialDateTime.minute,
- ),
- );
- return VAlertDialog(
- title: title,
- contentPadding: const EdgeInsets.symmetric(vertical: 0, horizontal: 16),
- content: _buildContent(context, valueCache),
- showCancel: true,
- onConfirm: () {
- Get.back(result: valueCache.value);
- },
- );
- }
- Widget _buildContent(BuildContext context, _VDialogDateValueCache cache) {
- // 不保留时间部分
- return Container(
- margin: const EdgeInsets.all(10),
- height: 216,
- //width: 800,
- child: CupertinoDatePicker(
- onDateTimeChanged: (DateTime value) {
- cache.value = value;
- },
- minimumYear: 1900,
- maximumYear: 2100,
- minimumDate: minimumDate,
- maximumDate: maxValue,
- backgroundColor: CupertinoColors.white,
- initialDateTime: cache.value,
- use24hFormat: true,
- ),
- );
- }
- }
|