import 'package:fis_i18n/i18n.dart'; import 'package:flutter/material.dart'; import 'package:flyinsonolite/controls/custom/customdatepicker.dart'; import 'package:flyinsonolite/controls/custom/customiconbutton.dart'; import 'package:flyinsonolite/controls/custom/customtextbutton.dart'; import 'package:flyinsonolite/controls/text/fistextfield.dart'; import 'package:flyinsonolite/infrastructure/scale.dart'; import 'package:flyinsonolite/infrastructure/storage.dart'; import 'package:intl/intl.dart'; class DateTimeTextField extends StatelessWidget { final TextEditingController _dateController = TextEditingController(); DateTime? selectedDate; String? placeHolder; TextStyle? textStyle; Color? borderColor; Color? enabledBorderColor; Color? fillColor; final ValueChanged? dateChanged; bool selectableDayCheck = false; DateTimeTextField(this.placeHolder, this.selectedDate, {super.key, this.textStyle, this.borderColor, this.enabledBorderColor, this.fillColor, this.dateChanged, this.selectableDayCheck = false}); @override Widget build(BuildContext context) { if (selectedDate != null) { _dateController.text = DateFormat('yyyy-MM-dd').format(selectedDate!); } return FISTextField( uniqueKey: null, contentPadding: EdgeInsets.fromLTRB(10.s, 0, 0, 0), textStyle: textStyle, readOnly: true, controller: _dateController, placeHolder: placeHolder, borderColor: borderColor, enabledBorderColor: enabledBorderColor, fillColor: fillColor, onTap: () async { await _showDatePickerAsync(context); }, suffixIcon: CustomIconButton( constraints: const BoxConstraints(), padding: const EdgeInsets.all(0), onPressed: () async { await _showDatePickerAsync(context); }, icon: Icon( Icons.date_range_rounded, size: 24.s, color: Storage.currentTheme.themeData.indicatorColor, ), )); } Future _showDatePickerAsync(BuildContext context) async { var pickedDate = await showCustomDatePicker( context: context, initialEntryMode: DatePickerEntryMode.calendarOnly, initialDate: selectedDate ?? DateTime.now(), firstDate: DateTime(2000), lastDate: DateTime(4000), locale: i18nBook.locale, currentDate: DateTime.now(), selectableDayPredicate: (day) { if (selectableDayCheck) { var earlyDatetime = DateTime.now(); if (selectedDate != null) { if (selectedDate!.difference(DateTime.now()).inDays < 0) { earlyDatetime = selectedDate!; } } return day.difference(earlyDatetime).inDays >= 0; } else { return true; } }, builder: (context, child) { return Theme( data: ThemeData( fontFamily: Storage.currentTheme.fontFamily, // 更改右侧时间字体的大小 textTheme: TextTheme( titleSmall: TextStyle( fontSize: 18.s, color: const Color.fromRGBO(211, 211, 211, 1)), titleMedium: TextStyle( fontSize: 18.s, color: const Color.fromRGBO(211, 211, 211, 1)), titleLarge: TextStyle( fontSize: 18.s, color: const Color.fromRGBO(211, 211, 211, 1)), bodySmall: TextStyle( fontSize: 18.s, color: const Color.fromRGBO(211, 211, 211, 1)), bodyMedium: TextStyle( fontSize: 18.s, color: const Color.fromRGBO(211, 211, 211, 1)), bodyLarge: TextStyle( fontSize: 18.s, color: const Color.fromRGBO(211, 211, 211, 1)), labelSmall: TextStyle( fontSize: 18.s, color: const Color.fromRGBO(211, 211, 211, 1), ), labelMedium: TextStyle( fontSize: 18.s, color: const Color.fromRGBO(211, 211, 211, 1)), labelLarge: TextStyle( fontSize: 18.s, color: const Color.fromRGBO(211, 211, 211, 1)), headlineSmall: TextStyle( fontSize: 20.s, color: const Color.fromRGBO(211, 211, 211, 1), ), headlineMedium: TextStyle( fontSize: 20.s, color: const Color.fromRGBO(211, 211, 211, 1), ), headlineLarge: TextStyle( fontSize: 20.s, color: const Color.fromRGBO(211, 211, 211, 1), ), displaySmall: TextStyle( fontSize: 20.s, color: const Color.fromRGBO(211, 211, 211, 1), ), displayMedium: TextStyle( fontSize: 20.s, color: const Color.fromRGBO(211, 211, 211, 1), ), displayLarge: TextStyle( fontSize: 20.s, color: const Color.fromRGBO(211, 211, 211, 1), ), ), inputDecorationTheme: InputDecorationTheme( labelStyle: TextStyle( fontSize: 22.s, color: const Color.fromRGBO(211, 211, 211, 1), ), ), dialogBackgroundColor: Storage.currentTheme.datePickerStyle.backgroundColor, colorScheme: ColorScheme.light( primary: Storage.currentTheme.datePickerStyle.leftSizeColor, onSurface: Storage.currentTheme.normalTextStyle.color!), textButtonTheme: TextButtonThemeData( style: CustomTextButton.styleFrom( foregroundColor: Storage.currentTheme.normalTextStyle.color, textStyle: Storage.currentTheme.normalTextStyle))), child: child!); }, ); if (pickedDate != null) { selectedDate = pickedDate; _dateController.text = DateFormat('yyyy-MM-dd').format(pickedDate); dateChanged!(pickedDate); } } }