You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.4 KiB

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
class DatePickerButton extends StatelessWidget {
DatePickerButton({super.key});
final DatePickerController dateController = Get.put(DatePickerController());
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
ElevatedButton(
style: ElevatedButton.styleFrom(
padding: EdgeInsets.symmetric(horizontal: 16, vertical: 12),
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
elevation: 2,
),
onPressed: () {
TDPicker.showDatePicker(
context,
title: '选择时间',
onConfirm: (selected) {
dateController.updateDateTime(selected);
Get.back();
},
useHour: true,
useMinute: true,
useSecond: true,
dateStart: [1999, 01, 01],
dateEnd: [2029, 12, 31],
initialDate: [2025, 1, 1],
);
},
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
Obx(
() => Text(
dateController.selectedDateTime.value.isEmpty
? "选择日期"
: dateController.selectedDateTime.value,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
),
SizedBox(width: 8),
Icon(Icons.keyboard_arrow_down, color: Colors.black),
],
),
),
],
);
}
}
class DatePickerController extends GetxController {
var selectedDateTime = ''.obs;
void updateDateTime(Map<String, int> selected) {
selectedDateTime.value =
'${selected['year'].toString().padLeft(4, '0')}-'
'${selected['month'].toString().padLeft(2, '0')}-'
'${selected['day'].toString().padLeft(2, '0')} ';
// '${selected['hour'].toString().padLeft(2, '0')}:'
// '${selected['minute'].toString().padLeft(2, '0')}:'
// '${selected['second'].toString().padLeft(2, '0')}';
}
}