12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import 'package:calendar_date_picker2/calendar_date_picker2.dart';
- import 'package:date_format/date_format.dart';
- import 'package:flutter/material.dart';
- class VDatePicker extends StatefulWidget {
- final DateTime? value;
- final ValueChanged<DateTime>? onChanged;
- const VDatePicker({super.key, this.value, this.onChanged});
- @override
- State<StatefulWidget> createState() => _DatePickerState();
- }
- class _DatePickerState extends State<VDatePicker> {
- late DateTime currentDate;
- @override
- void initState() {
- if (widget.value == null) {
- final now = DateTime.now();
- currentDate = DateTime(now.year, now.month, now.day);
- } else {
- currentDate = widget.value!;
- }
- super.initState();
- }
- @override
- Widget build(BuildContext context) {
- final dateStr = formatDate(currentDate, ["yyyy", "-", "mm", "-", "dd"]);
- return Material(
- child: Ink(
- child: InkWell(
- onTap: () async {
- var results = await showCalendarDatePicker2Dialog(
- context: context,
- config: CalendarDatePicker2WithActionButtonsConfig(
- calendarType: CalendarDatePicker2Type.single,
- ),
- dialogBackgroundColor: Colors.white,
- dialogSize: const Size(325, 400),
- value: [
- currentDate,
- ],
- borderRadius: BorderRadius.circular(4),
- );
- if (results != null &&
- results.isNotEmpty &&
- results.first != null) {
- setState(() {
- currentDate = results.first!;
- widget.onChanged?.call(currentDate);
- });
- }
- },
- child: Container(
- // height: 38,
- alignment: Alignment.centerLeft,
- padding: const EdgeInsets.symmetric(horizontal: 8),
- decoration: BoxDecoration(
- border: Border.all(color: Colors.grey),
- borderRadius: BorderRadius.zero,
- ),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: [
- Text(
- dateStr,
- style: const TextStyle(color: Colors.black, fontSize: 16),
- ),
- const Icon(
- Icons.calendar_month_outlined,
- color: Colors.grey,
- size: 18,
- ),
- ],
- ),
- ),
- ),
- ),
- );
- }
- }
|