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.

170 lines
6.7KB

  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 void 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. @override
  23. _FieldDateWidgetState createState() => _FieldDateWidgetState();
  24. }
  25. class _FieldDateWidgetState extends State<FieldDateWidget> {
  26. ChangeDateTimePicker controller;
  27. final textController = TextEditingController();
  28. var isShowError = false;
  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: Validators.stringNotNullOrEmpty(textController.text) &&
  45. validators != null
  46. ? 65
  47. : widget.isValidated
  48. ? 85
  49. : 85,
  50. child: Column(
  51. crossAxisAlignment: CrossAxisAlignment.start,
  52. children: [
  53. Validators.stringNotNullOrEmpty(
  54. data?.selectedDateTime?.displayDateTime_DDMMYYYY_HHmm())
  55. ? Text(
  56. widget.hint ?? '',
  57. style: TextStyle(
  58. color: Validators.stringNotNullOrEmpty(
  59. textController.text) &&
  60. validators != null
  61. ? Colors.black54
  62. : Colors.red,
  63. fontSize: 13.0),
  64. )
  65. : Text(
  66. '',
  67. style: TextStyle(color: Colors.black54, fontSize: 13.0),
  68. ),
  69. SizedBox(
  70. width: double.infinity,
  71. height: 44,
  72. child: FlatButton(
  73. padding: EdgeInsets.only(
  74. top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  75. onPressed: () {
  76. DatePicker.showDateTimePicker(context,
  77. showTitleActions: true,
  78. onChanged: (date) {}, onConfirm: (date) {
  79. controller.change(date);
  80. widget.onPressed(date);
  81. textController.text = date.toString();
  82. },
  83. currentTime: data?.selectedDateTime,
  84. locale: LocaleType.vi);
  85. },
  86. child: Column(
  87. crossAxisAlignment: CrossAxisAlignment.start,
  88. children: [
  89. Container(
  90. padding: EdgeInsets.only(
  91. top: 0.0,
  92. right: 0.0,
  93. bottom: 10.5,
  94. left: 0.0),
  95. decoration: BoxDecoration(
  96. border: Border(
  97. bottom: BorderSide(
  98. width: 0.5,
  99. color: Validators.stringNotNullOrEmpty(
  100. textController.text) &&
  101. validators != null
  102. ? Colors.black54
  103. : widget.isValidated
  104. ? Colors.red
  105. : Colors.black54,
  106. )),
  107. ),
  108. child: Row(
  109. children: [
  110. Expanded(
  111. child: Validators.stringNotNullOrEmpty(data
  112. ?.selectedDateTime
  113. ?.displayDateTime_DDMMYYYY_HHmm())
  114. ? Text(
  115. data?.selectedDateTime
  116. ?.displayDateTime_DDMMYYYY_HHmm(),
  117. style: TextStyle(
  118. fontSize: 16.0,
  119. color: Colors.black))
  120. : Text(widget.hint,
  121. style: TextStyle(
  122. fontSize: 16.0,
  123. color: Colors.black54)),
  124. ),
  125. Icon(
  126. Icons.date_range,
  127. color: Colors.grey,
  128. ),
  129. ],
  130. ))
  131. ],
  132. )),
  133. ),
  134. Validators.stringNotNullOrEmpty(textController.text) &&
  135. validators != null
  136. ? SizedBox()
  137. : Text(
  138. widget.isValidated
  139. ? 'Vui lòng nhập ${widget.hint}'
  140. : '',
  141. style: TextStyle(
  142. fontSize: 12.0,
  143. color: Colors.red,
  144. fontWeight: FontWeight.normal),
  145. textAlign: TextAlign.left,
  146. ),
  147. Container(
  148. height: 0,
  149. child: TextFormField(
  150. style: TextStyle(color: Colors.white),
  151. decoration: InputDecoration(
  152. border: InputBorder.none,
  153. labelStyle: TextStyle(color: Colors.white)),
  154. validator: widget.validator,
  155. controller: textController,
  156. ),
  157. )
  158. ],
  159. ),
  160. );
  161. });
  162. }
  163. }