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.

132 lines
4.5KB

  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  3. import 'package:flutter_screenutil/flutter_screenutil.dart';
  4. import 'package:flutter_svg/svg.dart';
  5. import 'package:intl/intl.dart';
  6. import '../../../themes/app_colors.dart';
  7. import '../../../themes/styles_text.dart';
  8. import '../../../utils/app_images.dart';
  9. class DatePickerWidget extends StatefulWidget {
  10. final bool isEnable;
  11. final DateTime initDateTime;
  12. final DateTime? minDate;
  13. final bool isMinDate;
  14. SvgPicture? icon;
  15. final bool isAcceptSelectPassDate;
  16. final bool isShowTime;
  17. final Function(DateTime? selectedDateTimeLocal) onUpdateDateTime;
  18. DatePickerWidget({
  19. required this.initDateTime,
  20. required this.onUpdateDateTime,
  21. this.minDate,
  22. this.isMinDate = false,
  23. this.isEnable = true,
  24. this.icon,
  25. this.isAcceptSelectPassDate = true,
  26. this.isShowTime = false,
  27. });
  28. @override
  29. _DatePickerWidgetState createState() => _DatePickerWidgetState();
  30. }
  31. class _DatePickerWidgetState extends State<DatePickerWidget> {
  32. late DateTime selectedDate;
  33. @override
  34. void initState() {
  35. super.initState();
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. selectedDate = widget.initDateTime;
  40. print('--- min date ----');
  41. print(widget.isMinDate ? DateTime.parse(widget.minDate.toString()) : DateTime(DateTime.now().year - 5));
  42. return InkWell(
  43. onTap: widget.isEnable
  44. ? () {
  45. widget.isShowTime
  46. ? DatePicker.showDateTimePicker(
  47. context,
  48. showTitleActions: true,
  49. minTime: DateTime(DateTime.now().year - 5),
  50. maxTime: DateTime(DateTime.now().year + 5),
  51. onChanged: (date) {},
  52. onConfirm: (date) {
  53. if (date != null) {
  54. if (widget.isAcceptSelectPassDate) {
  55. } else {
  56. var now = DateTime.now();
  57. if (date.isBefore(
  58. DateTime(now.year, now.month, now.day, 00, 00, 00),
  59. )) {
  60. return;
  61. }
  62. }
  63. widget.onUpdateDateTime(date);
  64. setState(() {
  65. selectedDate = date;
  66. });
  67. }
  68. },
  69. currentTime: selectedDate,
  70. locale: LocaleType.vi,
  71. )
  72. : showDatePicker(
  73. context: context,
  74. initialDate: selectedDate,
  75. firstDate: DateTime(DateTime.now().year - 5),
  76. lastDate: DateTime(DateTime.now().year + 5),
  77. ).then((date) {
  78. if (date != null) {
  79. if (widget.isAcceptSelectPassDate) {
  80. } else {
  81. var now = DateTime.now();
  82. if (date.isBefore(
  83. DateTime(now.year, now.month, now.day, 00, 00, 00),
  84. )) {
  85. return;
  86. }
  87. }
  88. widget.onUpdateDateTime(date);
  89. setState(() {
  90. selectedDate = date;
  91. });
  92. }
  93. });
  94. }
  95. : null,
  96. child: Container(
  97. height: 48.h,
  98. decoration: BoxDecoration(
  99. borderRadius: BorderRadius.circular(12),
  100. border: Border.all(
  101. color: AppColors.neutral4,
  102. width: 1,
  103. ),
  104. color: widget.isEnable ? Colors.white : Colors.transparent,
  105. ),
  106. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
  107. child: Row(
  108. children: [
  109. Expanded(
  110. child: Text(
  111. widget.isShowTime ? DateFormat('dd/MM/yyyy HH:mm').format(selectedDate) : DateFormat('dd/MM/yyyy').format(selectedDate),
  112. style: StylesText.body6,
  113. textAlign: TextAlign.start,
  114. ),
  115. ),
  116. SizedBox(
  117. child: widget.icon ??
  118. SvgPicture.asset(
  119. AssetSVG.icCalendar,
  120. ),
  121. )
  122. ],
  123. ),
  124. ),
  125. );
  126. }
  127. }