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.

145 lines
5.3KB

  1. import 'package:farm_tpf/utils/validators.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_datetime_picker_plus/flutter_datetime_picker_plus.dart' as datatTimePicker;
  4. import 'package:get/get.dart';
  5. import 'package:get/get_state_manager/get_state_manager.dart';
  6. import 'package:farm_tpf/utils/formatter.dart';
  7. import 'widget_field_time_picker.dart';
  8. class FieldDateWidget extends StatefulWidget {
  9. final String hint;
  10. final String? value;
  11. final Function(DateTime) onPressed;
  12. final String tag;
  13. final String? Function(String?)? validator;
  14. final bool? isValidated;
  15. FieldDateWidget({
  16. required this.hint,
  17. required this.tag,
  18. this.value,
  19. required this.onPressed,
  20. this.validator,
  21. this.isValidated,
  22. });
  23. @override
  24. _FieldDateWidgetState createState() => _FieldDateWidgetState();
  25. }
  26. class _FieldDateWidgetState extends State<FieldDateWidget> {
  27. ChangeDateTimePicker? controller;
  28. final textController = TextEditingController();
  29. @override
  30. void initState() {
  31. super.initState();
  32. controller = Get.put(ChangeDateTimePicker(), tag: widget.tag);
  33. textController.addListener(() {
  34. print('object');
  35. });
  36. }
  37. @override
  38. Widget build(BuildContext context) {
  39. return GetBuilder<ChangeDateTimePicker>(
  40. init: controller,
  41. tag: widget.tag,
  42. builder: (data) {
  43. return SizedBox(
  44. width: double.infinity,
  45. height: isShowError() ? 85 : 65,
  46. child: Column(
  47. crossAxisAlignment: CrossAxisAlignment.start,
  48. children: [
  49. isShowError()
  50. ? Text(
  51. widget.hint ?? '',
  52. style: TextStyle(color: Colors.red, fontSize: 13.0),
  53. )
  54. : Text(
  55. '',
  56. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  57. ),
  58. SizedBox(
  59. width: double.infinity,
  60. height: 44,
  61. child: TextButton(
  62. onPressed: () {
  63. datatTimePicker.DatePicker.showDateTimePicker(
  64. context,
  65. showTitleActions: true,
  66. onChanged: (date) {},
  67. onConfirm: (date) {
  68. controller?.change(date);
  69. widget.onPressed(date);
  70. textController.text = date.toString();
  71. },
  72. currentTime: data?.selectedDateTime,
  73. locale: datatTimePicker.LocaleType.vi,
  74. );
  75. },
  76. child: Column(
  77. crossAxisAlignment: CrossAxisAlignment.start,
  78. children: [
  79. Container(
  80. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  81. decoration: BoxDecoration(
  82. border: Border(
  83. bottom: BorderSide(
  84. width: 0.5,
  85. color: isShowError() ? Colors.red : Colors.black54,
  86. )),
  87. ),
  88. child: Row(
  89. children: [
  90. Expanded(
  91. child: Validators.stringNotNullOrEmpty(data?.selectedDateTime?.displayDateTime_DDMMYYYY_HHmm() ?? '')
  92. ? Text(data?.selectedDateTime?.displayDateTime_DDMMYYYY_HHmm() ?? '',
  93. style: TextStyle(fontSize: 16.0, color: Colors.black))
  94. : Text(widget.hint, style: TextStyle(fontSize: 16.0, color: Colors.black54)),
  95. ),
  96. Icon(
  97. Icons.date_range,
  98. color: Colors.grey,
  99. ),
  100. ],
  101. ))
  102. ],
  103. )),
  104. ),
  105. isShowError()
  106. ? Text(
  107. (widget.isValidated ?? false) ? 'Vui lòng nhập ${widget.hint}' : '',
  108. style: TextStyle(fontSize: 12.0, color: Colors.red, fontWeight: FontWeight.normal),
  109. textAlign: TextAlign.left,
  110. )
  111. : SizedBox(),
  112. Container(
  113. height: 0,
  114. child: TextFormField(
  115. style: TextStyle(color: Colors.white),
  116. decoration: InputDecoration(border: InputBorder.none, labelStyle: TextStyle(color: Colors.white)),
  117. validator: widget.validator,
  118. controller: textController,
  119. ),
  120. )
  121. ],
  122. ),
  123. );
  124. });
  125. }
  126. bool isShowError() {
  127. if (widget.validator == null) {
  128. return false;
  129. } else {
  130. if (widget.isValidated ?? false) {
  131. return Validators.stringNotNullOrEmpty(textController.text) ? false : true;
  132. } else {
  133. return false;
  134. }
  135. }
  136. }
  137. }