123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- 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;
- final CupertinoDatePickerMode? mode;
- const VDialogDateTime({
- super.key,
- this.title,
- this.initialValue,
- this.minimumDate,
- this.maxValue,
- this.mode,
- });
- 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,
- mode: mode ?? CupertinoDatePickerMode.dateAndTime,
- ),
- );
- }
- }
- /// 弹窗日期选择器
- class VDialogDateTime2 extends StatelessWidget {
- /// 标题
- final String? title;
- /// 初始值
- final DateTime? initialValue;
- /// 最小时间
- final DateTime? minimumDate;
- /// 最大值
- final DateTime? maxValue;
- const VDialogDateTime2({
- 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;
- },
- mode: CupertinoDatePickerMode.time,
- minimumYear: 1900,
- maximumYear: 2100,
- minimumDate: minimumDate,
- maximumDate: maxValue,
- backgroundColor: CupertinoColors.white,
- initialDateTime: cache.value,
- use24hFormat: true,
- ),
- );
- }
- }
|