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.

205 lines
8.7KB

  1. import 'package:farm_tpf/custom_model/action_form/RequestActivity.dart';
  2. import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
  3. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeFieldInForm.dart';
  4. import 'package:farm_tpf/presentation/screens/actions/controller/ChangeWorker.dart';
  5. import 'package:farm_tpf/utils/const_string.dart';
  6. import 'package:farm_tpf/utils/validators.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:get/get.dart';
  9. import 'package:get/get_state_manager/get_state_manager.dart';
  10. class WidgetWorker extends StatefulWidget {
  11. final Function(List<TbNurseryDetailsDTO>) onChangeWorkers;
  12. const WidgetWorker({required this.onChangeWorkers});
  13. @override
  14. _WidgetWorkerState createState() => _WidgetWorkerState();
  15. }
  16. class _WidgetWorkerState extends State<WidgetWorker> {
  17. GlobalKey<FormState> _formWorkerKey = GlobalKey();
  18. TextEditingController _workerNameController = TextEditingController();
  19. TextEditingController _trayNumberController = TextEditingController();
  20. ChangeWorker? controller;
  21. final changeFormField = Get.put(ChangeFieldFormSupply());
  22. @override
  23. void initState() {
  24. super.initState();
  25. controller = Get.put(ChangeWorker());
  26. controller?.init([]);
  27. changeFormField.init();
  28. }
  29. Widget _buildListWorker() {
  30. return GetBuilder<ChangeWorker>(
  31. init: controller,
  32. builder: (data) {
  33. widget.onChangeWorkers(data.currentItems ?? []);
  34. if (data.currentItems?.length == 0 || data.currentItems == null) {
  35. return Container();
  36. } else {
  37. return Container(
  38. height: 70,
  39. child: ListView.builder(
  40. physics: ClampingScrollPhysics(),
  41. scrollDirection: Axis.horizontal,
  42. shrinkWrap: true,
  43. itemCount: data?.currentItems?.length ?? 0,
  44. itemBuilder: (context, index) {
  45. return GestureDetector(
  46. onTap: () {
  47. print("edit worker");
  48. },
  49. child: Card(
  50. child: Stack(
  51. alignment: Alignment.bottomCenter,
  52. children: <Widget>[
  53. Positioned(
  54. child: ClipRRect(
  55. borderRadius: BorderRadius.circular(8),
  56. child: Container(
  57. padding: EdgeInsets.all(4),
  58. width: 120,
  59. child: Column(
  60. mainAxisAlignment: MainAxisAlignment.center,
  61. children: [
  62. SizedBox(
  63. height: 12.0,
  64. ),
  65. Flexible(
  66. child: Text(data.currentItems?[index].workerName ?? '', overflow: TextOverflow.ellipsis, maxLines: 1),
  67. ),
  68. Validators.stringNotNullOrEmpty(data.currentItems?[index].trayNumber ?? '')
  69. ? Flexible(child: Text(data.currentItems?[index].trayNumber ?? ''))
  70. : SizedBox()
  71. ],
  72. ),
  73. ),
  74. )),
  75. Positioned(
  76. top: -10,
  77. right: -10,
  78. child: IconButton(
  79. icon: Icon(
  80. Icons.cancel,
  81. color: Colors.redAccent,
  82. ),
  83. onPressed: () {
  84. controller?.deleteNurseryDetail(index);
  85. print("Delete worker");
  86. }),
  87. )
  88. ],
  89. )));
  90. }));
  91. }
  92. });
  93. }
  94. @override
  95. Widget build(BuildContext context) {
  96. return GetBuilder<ChangeWorker>(
  97. init: controller,
  98. builder: (data) {
  99. return Container(
  100. child: Form(
  101. key: _formWorkerKey,
  102. child: Column(
  103. children: [
  104. Container(
  105. width: double.infinity,
  106. height: 16,
  107. color: Colors.grey[200],
  108. ),
  109. SizedBox(
  110. height: 8.0,
  111. ),
  112. Padding(
  113. padding: const EdgeInsets.all(8.0),
  114. child: Align(
  115. alignment: Alignment.centerLeft,
  116. child: Text(
  117. 'Người thực hiện',
  118. style: TextStyle(color: Colors.black54, fontSize: 14),
  119. ),
  120. ),
  121. ),
  122. _buildListWorker(),
  123. Container(
  124. padding: EdgeInsets.all(8.0),
  125. margin: EdgeInsets.all(8),
  126. decoration: BoxDecoration(
  127. shape: BoxShape.rectangle,
  128. borderRadius: BorderRadius.circular(10),
  129. color: Colors.white,
  130. border: Border.all(color: Colors.grey)),
  131. child: Column(
  132. children: [
  133. TextFormField(
  134. keyboardType: TextInputType.text,
  135. controller: _workerNameController,
  136. decoration: InputDecoration(labelText: "Tên công nhân *"),
  137. validator: (value) {
  138. return Validators.validateNotNullOrEmpty(value ?? '', label_validate_input_empty);
  139. },
  140. onSaved: (newValue) {},
  141. onChanged: (value) {
  142. if (!Validators.stringNotNullOrEmpty(value)) {
  143. changeFormField.change(false);
  144. } else {
  145. changeFormField.change(true);
  146. }
  147. },
  148. ),
  149. TextFormField(
  150. keyboardType: TextInputType.text,
  151. controller: _trayNumberController,
  152. decoration: InputDecoration(labelText: "Ươm khây số"),
  153. onSaved: (newValue) {},
  154. onChanged: (value) {
  155. if (!Validators.stringNotNullOrEmpty(value)) {
  156. changeFormField.change(false);
  157. } else {
  158. changeFormField.change(true);
  159. }
  160. },
  161. ),
  162. ],
  163. ),
  164. ),
  165. Container(
  166. margin: EdgeInsets.all(8),
  167. child: TextButton(
  168. onPressed: () {
  169. if (_formWorkerKey.currentState!.validate()) {
  170. _formWorkerKey.currentState!.save();
  171. if (Validators.stringNotNullOrEmpty(_workerNameController.text)) {
  172. TbNurseryDetailsDTO _nurseryDetail = TbNurseryDetailsDTO()
  173. ..workerName = _workerNameController.text
  174. ..trayNumber = _trayNumberController.text;
  175. controller?.addNurseryDetail(_nurseryDetail);
  176. _workerNameController.clear();
  177. _trayNumberController.clear();
  178. changeFormField.change(false);
  179. } else {
  180. Utils.showSnackBarWarning(message: "Vui lòng nhập tên công nhân");
  181. }
  182. } else {
  183. //
  184. if (!Validators.stringNotNullOrEmpty(_workerNameController.text)) {
  185. Utils.showSnackBarWarning(message: "Vui lòng nhập tên công nhân");
  186. }
  187. }
  188. },
  189. child: Text(
  190. "+ Thêm người thực hiện",
  191. style: TextStyle(color: Colors.blue),
  192. )),
  193. ),
  194. ],
  195. ),
  196. ));
  197. });
  198. }
  199. }