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.

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