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.

137 lines
5.1KB

  1. import 'package:farm_tpf/utils/validators.dart';
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  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. tag: widget.tag,
  41. builder: (data) {
  42. return SizedBox(
  43. width: double.infinity,
  44. height: isShowError() ? 85 : 65,
  45. child: Column(
  46. crossAxisAlignment: CrossAxisAlignment.start,
  47. children: [
  48. isShowError()
  49. ? Text(
  50. widget.hint ?? '',
  51. style: TextStyle(color: Colors.red, fontSize: 13.0),
  52. )
  53. : Text(
  54. '',
  55. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  56. ),
  57. SizedBox(
  58. width: double.infinity,
  59. height: 44,
  60. child: TextButton(
  61. onPressed: () {
  62. DatePicker.showDateTimePicker(context, showTitleActions: true, onChanged: (date) {}, onConfirm: (date) {
  63. controller?.change(date);
  64. widget.onPressed(date);
  65. textController.text = date.toString();
  66. }, currentTime: data?.selectedDateTime, locale: LocaleType.vi);
  67. },
  68. child: Column(
  69. crossAxisAlignment: CrossAxisAlignment.start,
  70. children: [
  71. Container(
  72. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  73. decoration: BoxDecoration(
  74. border: Border(
  75. bottom: BorderSide(
  76. width: 0.5,
  77. color: isShowError() ? Colors.red : Colors.black54,
  78. )),
  79. ),
  80. child: Row(
  81. children: [
  82. Expanded(
  83. child: Validators.stringNotNullOrEmpty(data?.selectedDateTime?.displayDateTime_DDMMYYYY_HHmm() ?? '')
  84. ? Text(data?.selectedDateTime?.displayDateTime_DDMMYYYY_HHmm() ?? '',
  85. style: TextStyle(fontSize: 16.0, color: Colors.black))
  86. : Text(widget.hint, style: TextStyle(fontSize: 16.0, color: Colors.black54)),
  87. ),
  88. Icon(
  89. Icons.date_range,
  90. color: Colors.grey,
  91. ),
  92. ],
  93. ))
  94. ],
  95. )),
  96. ),
  97. isShowError()
  98. ? Text(
  99. (widget.isValidated ?? false) ? 'Vui lòng nhập ${widget.hint}' : '',
  100. style: TextStyle(fontSize: 12.0, color: Colors.red, fontWeight: FontWeight.normal),
  101. textAlign: TextAlign.left,
  102. )
  103. : SizedBox(),
  104. Container(
  105. height: 0,
  106. child: TextFormField(
  107. style: TextStyle(color: Colors.white),
  108. decoration: InputDecoration(border: InputBorder.none, labelStyle: TextStyle(color: Colors.white)),
  109. validator: widget.validator,
  110. controller: textController,
  111. ),
  112. )
  113. ],
  114. ),
  115. );
  116. });
  117. }
  118. bool isShowError() {
  119. if (widget.validator == null) {
  120. return false;
  121. } else {
  122. if (widget.isValidated ?? false) {
  123. return Validators.stringNotNullOrEmpty(textController.text) ? false : true;
  124. } else {
  125. return false;
  126. }
  127. }
  128. }
  129. }