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.

328 lines
12KB

  1. import 'package:farm_tpf/custom_model/CropPlot.dart';
  2. import 'package:farm_tpf/data/api/app_exception.dart';
  3. import 'package:farm_tpf/data/repository/repository.dart';
  4. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/widget_toast.dart';
  7. import 'package:farm_tpf/presentation/screens/plot_detail/bloc_plot_information.dart';
  8. import 'package:farm_tpf/utils/const_color.dart';
  9. import 'package:farm_tpf/utils/const_string.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:fluttertoast/fluttertoast.dart';
  12. import 'package:get/get.dart';
  13. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  14. import 'package:farm_tpf/utils/formatter.dart';
  15. class PlotInformationScreen extends StatefulWidget {
  16. final int cropId;
  17. PlotInformationScreen({@required this.cropId});
  18. @override
  19. _PlotInformationScreenState createState() => _PlotInformationScreenState();
  20. }
  21. class _PlotInformationScreenState extends State<PlotInformationScreen> {
  22. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  23. GlobalKey<FormState> _formKey = GlobalKey();
  24. TextEditingController _statusController = TextEditingController();
  25. TextEditingController _houseNameController = TextEditingController();
  26. TextEditingController _codeController = TextEditingController();
  27. TextEditingController _supplyNameController = TextEditingController();
  28. TextEditingController _seedingDateController = TextEditingController();
  29. TextEditingController _timeSoakSeedController = TextEditingController();
  30. TextEditingController _timeNurserySeedController = TextEditingController();
  31. TextEditingController _quantityPlantController = TextEditingController();
  32. TextEditingController _currentPlantController = TextEditingController();
  33. TextEditingController _timeEndGrowController = TextEditingController();
  34. TextEditingController _mainTechnicianController = TextEditingController();
  35. TextEditingController _areaController = TextEditingController();
  36. TextEditingController _descriptionController = TextEditingController();
  37. FlutterToast flutterToast;
  38. bool _autoValidate = false;
  39. TbCropDTO _crop = TbCropDTO();
  40. final controller = Get.put(DescriptionChangeControler());
  41. Repository _repository = Repository();
  42. @override
  43. void initState() {
  44. super.initState();
  45. flutterToast = FlutterToast(context);
  46. getPlotInfoBloc.getPlotInfo(widget.cropId, (data) {
  47. var result = data as CropPlot;
  48. _crop = result.tbCropDTO;
  49. var statusCrop;
  50. switch (_crop.status) {
  51. case "STATUS_ARE_ACTIVE":
  52. statusCrop = plot_status_active;
  53. break;
  54. case "STATUS_FINISHED":
  55. statusCrop = plot_status_end;
  56. break;
  57. default:
  58. statusCrop = plot_status_unknown;
  59. }
  60. _statusController.text = statusCrop;
  61. _houseNameController.text = _crop.netHouseName.toString();
  62. _codeController.text = _crop.code.toString();
  63. _supplyNameController.text = _crop.suppliesName.toString();
  64. _areaController.text = _crop.areaM2.formatNumtoStringDecimal();
  65. _descriptionController.text =
  66. _crop.description == null ? "" : _crop.description.toString();
  67. }, (err) {});
  68. }
  69. _validateInputs() async {
  70. if (_formKey.currentState.validate()) {
  71. _formKey.currentState.save();
  72. LoadingDialog.showLoadingDialog(context);
  73. _repository.updatePlot(_crop).then((value) {
  74. LoadingDialog.hideLoadingDialog(context);
  75. flutterToast.showToast(
  76. child: WidgetToast(message: label_update_success));
  77. }).catchError((error) {
  78. _scaffoldKey.currentState.showSnackBar(SnackBar(
  79. content: Row(
  80. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  81. children: <Widget>[
  82. Flexible(child: Text(AppException.handleError(error))),
  83. Icon(Icons.error),
  84. ],
  85. ),
  86. backgroundColor: Colors.red,
  87. duration: Duration(seconds: 3),
  88. ));
  89. LoadingDialog.hideLoadingDialog(context);
  90. });
  91. } else {
  92. _autoValidate = true;
  93. }
  94. }
  95. Widget _statusField() {
  96. return TextFormField(
  97. controller: _statusController,
  98. decoration: InputDecoration(labelText: "Trạng thái"),
  99. );
  100. }
  101. Widget _houseNameField() {
  102. return TextFormField(
  103. controller: _houseNameController,
  104. decoration: InputDecoration(labelText: "Nhà màng"),
  105. );
  106. }
  107. Widget _codeField() {
  108. return TextFormField(
  109. controller: _codeController,
  110. decoration: InputDecoration(labelText: "Mã lô"),
  111. );
  112. }
  113. Widget _supplyNameField() {
  114. return TextFormField(
  115. controller: _supplyNameController,
  116. decoration: InputDecoration(labelText: "Giống"),
  117. );
  118. }
  119. Widget _seedingDateField() {
  120. return TextFormField(
  121. controller: _seedingDateController,
  122. decoration: InputDecoration(labelText: "Ngày gieo trồng"),
  123. );
  124. }
  125. Widget _timeSoakSeedField() {
  126. return TextFormField(
  127. controller: _timeSoakSeedController,
  128. decoration: InputDecoration(labelText: "Thời gian ngâm hạt"),
  129. );
  130. }
  131. Widget _timeNurserySeedField() {
  132. return TextFormField(
  133. controller: _timeNurserySeedController,
  134. decoration: InputDecoration(labelText: "Thời gian vô khây ươm"),
  135. );
  136. }
  137. Widget _quantityPlantField() {
  138. return TextFormField(
  139. controller: _quantityPlantController,
  140. decoration: InputDecoration(labelText: "Số lượng cây trồng"),
  141. );
  142. }
  143. Widget _currentPlantField() {
  144. return TextFormField(
  145. controller: _currentPlantController,
  146. decoration: InputDecoration(labelText: "Số lượng cây hiện tại"),
  147. );
  148. }
  149. Widget _timeEndGrowField() {
  150. return TextFormField(
  151. controller: _timeEndGrowController,
  152. decoration: InputDecoration(labelText: "Ngày kết thúc canh tác"),
  153. );
  154. }
  155. Widget _mainTechnicianField() {
  156. return TextFormField(
  157. controller: _mainTechnicianController,
  158. decoration: InputDecoration(labelText: "Kỹ sư trực tiếp"),
  159. );
  160. }
  161. Widget _areaField() {
  162. return TextFormField(
  163. controller: _areaController,
  164. decoration: InputDecoration(labelText: "Diện tích (m\u00B2)"),
  165. );
  166. }
  167. Widget _descriptionField() {
  168. return TextFormField(
  169. keyboardType: TextInputType.text,
  170. controller: _descriptionController,
  171. decoration: InputDecoration(labelText: "Ghi chú"),
  172. onSaved: (newValue) {
  173. _crop.description = newValue;
  174. },
  175. onChanged: (newValue) {
  176. controller.changeValue(_crop.description, newValue);
  177. },
  178. );
  179. }
  180. _actionAppBar() {
  181. return <Widget>[
  182. GetBuilder<DescriptionChangeControler>(
  183. builder: (_) {
  184. return IconButton(
  185. icon: Icon(
  186. Icons.done,
  187. ),
  188. disabledColor: Colors.grey,
  189. onPressed: controller.isChanged == false
  190. ? null
  191. : () {
  192. FocusScopeNode currentFocus = FocusScope.of(context);
  193. if (!currentFocus.hasPrimaryFocus) {
  194. currentFocus.unfocus();
  195. }
  196. _validateInputs();
  197. },
  198. );
  199. },
  200. )
  201. ];
  202. }
  203. @override
  204. Widget build(BuildContext context) => KeyboardDismisser(
  205. gestures: [
  206. GestureType.onTap,
  207. GestureType.onPanUpdateDownDirection,
  208. ],
  209. child: Container(
  210. color: COLOR_CONST.ITEM_BG,
  211. child: SafeArea(
  212. top: false,
  213. bottom: true,
  214. child: Scaffold(
  215. key: _scaffoldKey,
  216. appBar: AppBar(
  217. centerTitle: true,
  218. title: Text("Thông tin lô"),
  219. actions: _actionAppBar()),
  220. body: KeyboardDismisser(
  221. child: StreamBuilder(
  222. stream: getPlotInfoBloc.actions,
  223. builder: (BuildContext context,
  224. AsyncSnapshot<dynamic> snapshot) {
  225. if (snapshot.hasData) {
  226. return Form(
  227. key: _formKey,
  228. autovalidate: _autoValidate,
  229. child: SingleChildScrollView(
  230. padding: EdgeInsets.all(8.0),
  231. child: Column(
  232. children: <Widget>[
  233. _statusField(),
  234. SizedBox(
  235. height: 4.0,
  236. ),
  237. _houseNameField(),
  238. SizedBox(
  239. height: 4.0,
  240. ),
  241. _codeField(),
  242. SizedBox(
  243. height: 4.0,
  244. ),
  245. _supplyNameField(),
  246. SizedBox(
  247. height: 4.0,
  248. ),
  249. _seedingDateField(),
  250. SizedBox(
  251. height: 4.0,
  252. ),
  253. _timeSoakSeedField(),
  254. SizedBox(
  255. height: 4.0,
  256. ),
  257. _timeNurserySeedField(),
  258. SizedBox(
  259. height: 4.0,
  260. ),
  261. _quantityPlantField(),
  262. SizedBox(
  263. height: 4.0,
  264. ),
  265. _currentPlantField(),
  266. SizedBox(
  267. height: 4.0,
  268. ),
  269. _timeEndGrowField(),
  270. SizedBox(
  271. height: 4.0,
  272. ),
  273. _mainTechnicianField(),
  274. SizedBox(
  275. height: 4.0,
  276. ),
  277. _areaField(),
  278. SizedBox(
  279. height: 4.0,
  280. ),
  281. _descriptionField()
  282. ],
  283. ),
  284. ));
  285. } else if (snapshot.hasError) {
  286. return Center(
  287. child: Text(snapshot.error.toString()),
  288. );
  289. } else {
  290. return LoadingListPage();
  291. }
  292. }))))));
  293. }
  294. class DescriptionChangeControler extends GetxController {
  295. bool isChanged = false;
  296. void changeValue(String oldValue, String newValue) {
  297. if (oldValue != newValue) {
  298. isChanged = true;
  299. } else {
  300. isChanged = false;
  301. }
  302. update();
  303. }
  304. }