datetimetextfield.dart 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import 'package:fis_i18n/i18n.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flyinsonolite/controls/custom/customdatepicker.dart';
  4. import 'package:flyinsonolite/controls/custom/customiconbutton.dart';
  5. import 'package:flyinsonolite/controls/custom/customtextbutton.dart';
  6. import 'package:flyinsonolite/controls/text/fistextfield.dart';
  7. import 'package:flyinsonolite/infrastructure/scale.dart';
  8. import 'package:flyinsonolite/infrastructure/storage.dart';
  9. import 'package:intl/intl.dart';
  10. class DateTimeTextField extends StatelessWidget {
  11. final TextEditingController _dateController = TextEditingController();
  12. DateTime? selectedDate;
  13. String? placeHolder;
  14. TextStyle? textStyle;
  15. Color? borderColor;
  16. Color? enabledBorderColor;
  17. Color? fillColor;
  18. final ValueChanged<DateTime?>? dateChanged;
  19. bool selectableDayCheck = false;
  20. DateTimeTextField(this.placeHolder, this.selectedDate,
  21. {super.key,
  22. this.textStyle,
  23. this.borderColor,
  24. this.enabledBorderColor,
  25. this.fillColor,
  26. this.dateChanged,
  27. this.selectableDayCheck = false});
  28. @override
  29. Widget build(BuildContext context) {
  30. if (selectedDate != null) {
  31. _dateController.text = DateFormat('yyyy-MM-dd').format(selectedDate!);
  32. }
  33. return FISTextField(
  34. uniqueKey: null,
  35. contentPadding: EdgeInsets.fromLTRB(10.s, 0, 0, 0),
  36. textStyle: textStyle,
  37. readOnly: true,
  38. controller: _dateController,
  39. placeHolder: placeHolder,
  40. borderColor: borderColor,
  41. enabledBorderColor: enabledBorderColor,
  42. fillColor: fillColor,
  43. onTap: () async {
  44. await _showDatePickerAsync(context);
  45. },
  46. suffixIcon: CustomIconButton(
  47. constraints: const BoxConstraints(),
  48. padding: const EdgeInsets.all(0),
  49. onPressed: () async {
  50. await _showDatePickerAsync(context);
  51. },
  52. icon: Icon(
  53. Icons.date_range_rounded,
  54. size: 24.s,
  55. color: Storage.currentTheme.themeData.indicatorColor,
  56. ),
  57. ));
  58. }
  59. Future _showDatePickerAsync(BuildContext context) async {
  60. var pickedDate = await showCustomDatePicker(
  61. context: context,
  62. initialEntryMode: DatePickerEntryMode.calendarOnly,
  63. initialDate: selectedDate ?? DateTime.now(),
  64. firstDate: DateTime(2000),
  65. lastDate: DateTime(4000),
  66. locale: i18nBook.locale,
  67. currentDate: DateTime.now(),
  68. selectableDayPredicate: (day) {
  69. if (selectableDayCheck) {
  70. var earlyDatetime = DateTime.now();
  71. if (selectedDate != null) {
  72. if (selectedDate!.difference(DateTime.now()).inDays < 0) {
  73. earlyDatetime = selectedDate!;
  74. }
  75. }
  76. return day.difference(earlyDatetime).inDays >= 0;
  77. } else {
  78. return true;
  79. }
  80. },
  81. builder: (context, child) {
  82. return Theme(
  83. data: ThemeData(
  84. fontFamily: Storage.currentTheme.fontFamily,
  85. // 更改右侧时间字体的大小
  86. textTheme: TextTheme(
  87. titleSmall: TextStyle(
  88. fontSize: 18.s,
  89. color: const Color.fromRGBO(211, 211, 211, 1)),
  90. titleMedium: TextStyle(
  91. fontSize: 18.s,
  92. color: const Color.fromRGBO(211, 211, 211, 1)),
  93. titleLarge: TextStyle(
  94. fontSize: 18.s,
  95. color: const Color.fromRGBO(211, 211, 211, 1)),
  96. bodySmall: TextStyle(
  97. fontSize: 18.s,
  98. color: const Color.fromRGBO(211, 211, 211, 1)),
  99. bodyMedium: TextStyle(
  100. fontSize: 18.s,
  101. color: const Color.fromRGBO(211, 211, 211, 1)),
  102. bodyLarge: TextStyle(
  103. fontSize: 18.s,
  104. color: const Color.fromRGBO(211, 211, 211, 1)),
  105. labelSmall: TextStyle(
  106. fontSize: 18.s,
  107. color: const Color.fromRGBO(211, 211, 211, 1),
  108. ),
  109. labelMedium: TextStyle(
  110. fontSize: 18.s,
  111. color: const Color.fromRGBO(211, 211, 211, 1)),
  112. labelLarge: TextStyle(
  113. fontSize: 18.s,
  114. color: const Color.fromRGBO(211, 211, 211, 1)),
  115. headlineSmall: TextStyle(
  116. fontSize: 20.s,
  117. color: const Color.fromRGBO(211, 211, 211, 1),
  118. ),
  119. headlineMedium: TextStyle(
  120. fontSize: 20.s,
  121. color: const Color.fromRGBO(211, 211, 211, 1),
  122. ),
  123. headlineLarge: TextStyle(
  124. fontSize: 20.s,
  125. color: const Color.fromRGBO(211, 211, 211, 1),
  126. ),
  127. displaySmall: TextStyle(
  128. fontSize: 20.s,
  129. color: const Color.fromRGBO(211, 211, 211, 1),
  130. ),
  131. displayMedium: TextStyle(
  132. fontSize: 20.s,
  133. color: const Color.fromRGBO(211, 211, 211, 1),
  134. ),
  135. displayLarge: TextStyle(
  136. fontSize: 20.s,
  137. color: const Color.fromRGBO(211, 211, 211, 1),
  138. ),
  139. ),
  140. inputDecorationTheme: InputDecorationTheme(
  141. labelStyle: TextStyle(
  142. fontSize: 22.s,
  143. color: const Color.fromRGBO(211, 211, 211, 1),
  144. ),
  145. ),
  146. dialogBackgroundColor:
  147. Storage.currentTheme.datePickerStyle.backgroundColor,
  148. colorScheme: ColorScheme.light(
  149. primary: Storage.currentTheme.datePickerStyle.leftSizeColor,
  150. onSurface: Storage.currentTheme.normalTextStyle.color!),
  151. textButtonTheme: TextButtonThemeData(
  152. style: CustomTextButton.styleFrom(
  153. foregroundColor:
  154. Storage.currentTheme.normalTextStyle.color,
  155. textStyle: Storage.currentTheme.normalTextStyle))),
  156. child: child!);
  157. },
  158. );
  159. if (pickedDate != null) {
  160. selectedDate = pickedDate;
  161. _dateController.text = DateFormat('yyyy-MM-dd').format(pickedDate);
  162. dateChanged!(pickedDate);
  163. }
  164. }
  165. }