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.

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