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.

297 lines
15KB

  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/app_bar_widget.dart';
  5. import 'package:farm_tpf/presentation/custom_widgets/bloc/widget_row_plot_info.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/button_widget.dart';
  7. import 'package:farm_tpf/presentation/custom_widgets/loading_list_page.dart';
  8. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  9. import 'package:farm_tpf/presentation/custom_widgets/widget_utils.dart';
  10. import 'package:farm_tpf/presentation/screens/plot_detail/bloc_plot_information.dart';
  11. import 'package:farm_tpf/utils/const_color.dart';
  12. import 'package:farm_tpf/utils/const_string.dart';
  13. import 'package:flutter/material.dart';
  14. import 'package:get/get.dart';
  15. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  16. import 'package:farm_tpf/utils/formatter.dart';
  17. class PlotInformationScreen extends StatefulWidget {
  18. final int cropId;
  19. final bool isShowAppbar;
  20. PlotInformationScreen({@required this.cropId, this.isShowAppbar = false});
  21. @override
  22. _PlotInformationScreenState createState() => _PlotInformationScreenState();
  23. }
  24. class _PlotInformationScreenState extends State<PlotInformationScreen>
  25. with AutomaticKeepAliveClientMixin {
  26. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  27. GlobalKey<FormState> _formKey = GlobalKey();
  28. TextEditingController _descriptionController = TextEditingController();
  29. bool _autoValidate = false;
  30. CropPlot cropPlot = CropPlot();
  31. TbCropDTO _crop = TbCropDTO();
  32. final controller = Get.put(DescriptionChangeControler());
  33. Repository _repository = Repository();
  34. String statusCrop = plot_status_unknown;
  35. String technicians = '--';
  36. @override
  37. void initState() {
  38. super.initState();
  39. getPlotInfoBloc.getPlotInfo(widget.cropId, (data) {
  40. var result = data as CropPlot;
  41. cropPlot = result;
  42. _crop = result.tbCropDTO;
  43. switch (_crop.status) {
  44. case "STATUS_ARE_ACTIVE":
  45. statusCrop = plot_status_active;
  46. break;
  47. case "STATUS_FINISHED":
  48. statusCrop = plot_status_end;
  49. break;
  50. default:
  51. statusCrop = plot_status_unknown;
  52. }
  53. _descriptionController.text =
  54. _crop.description == null ? "" : _crop.description.toString();
  55. }, (err) {});
  56. }
  57. _validateInputs() async {
  58. if (_formKey.currentState.validate()) {
  59. _formKey.currentState.save();
  60. LoadingDialog.showLoadingDialog(context);
  61. _repository.updatePlot(_crop).then((value) {
  62. LoadingDialog.hideLoadingDialog(context);
  63. Utils.showSnackBarSuccess(message: label_update_success);
  64. controller.initValue();
  65. }).catchError((error) {
  66. LoadingDialog.hideLoadingDialog(context);
  67. Utils.showSnackBarError(message: AppException.handleError(error));
  68. });
  69. } else {
  70. _autoValidate = true;
  71. }
  72. }
  73. Widget _descriptionField() {
  74. return Container(
  75. padding: EdgeInsets.all(8),
  76. color: AppColors.YELLOW.withOpacity(0.1),
  77. child: TextFormField(
  78. keyboardType: TextInputType.text,
  79. controller: _descriptionController,
  80. decoration: InputDecoration(
  81. labelText: "Ghi chú",
  82. hintText: 'Ghi chú',
  83. enabledBorder: UnderlineInputBorder(
  84. borderSide: BorderSide(color: Colors.grey, width: 0.35))),
  85. onSaved: (newValue) {
  86. _crop.description = newValue;
  87. },
  88. onChanged: (newValue) {
  89. controller.changeValue(_crop.description, newValue);
  90. },
  91. ),
  92. );
  93. }
  94. @override
  95. Widget build(BuildContext context) => KeyboardDismisser(
  96. gestures: [
  97. GestureType.onTap,
  98. GestureType.onPanUpdateDownDirection,
  99. ],
  100. child: Container(
  101. color: AppColors.ITEM_BG,
  102. child: SafeArea(
  103. top: false,
  104. bottom: true,
  105. child: Scaffold(
  106. appBar: widget.isShowAppbar
  107. ? AppBarWidget()
  108. : PreferredSize(
  109. preferredSize: Size(0, 0),
  110. child: SizedBox(),
  111. ),
  112. key: _scaffoldKey,
  113. body: KeyboardDismisser(
  114. child: StreamBuilder(
  115. stream: getPlotInfoBloc.actions,
  116. builder: (BuildContext context,
  117. AsyncSnapshot<dynamic> snapshot) {
  118. if (snapshot.hasData) {
  119. return Form(
  120. key: _formKey,
  121. autovalidate: _autoValidate,
  122. child: SingleChildScrollView(
  123. padding: EdgeInsets.all(8.0),
  124. child: Column(
  125. children: <Widget>[
  126. WidgetRowPlotInfo(
  127. color: AppColors.DEFAULT
  128. .withOpacity(0.1),
  129. name: 'Mã lô',
  130. value:
  131. '${cropPlot.tbCropDTO.code}'),
  132. WidgetRowPlotInfo(
  133. color: AppColors.DEFAULT
  134. .withOpacity(0.3),
  135. name: 'Trạng thái',
  136. value: '$statusCrop'),
  137. WidgetRowPlotInfo(
  138. color: AppColors.DEFAULT
  139. .withOpacity(0.1),
  140. name: 'Nhà màng',
  141. value:
  142. '${cropPlot.tbCropDTO.netHouseName ?? '--'}'),
  143. WidgetRowPlotInfo(
  144. color: AppColors.DEFAULT
  145. .withOpacity(0.3),
  146. name: 'Giống',
  147. value:
  148. '${cropPlot.tbCropDTO.suppliesName ?? '--'}'),
  149. WidgetRowPlotInfo(
  150. color: AppColors.DEFAULT
  151. .withOpacity(0.1),
  152. name: 'Ngày gieo trồng',
  153. value:
  154. '${cropPlot.sowingDate ?? '--'}'),
  155. WidgetRowPlotInfo(
  156. color: AppColors.DEFAULT
  157. .withOpacity(0.3),
  158. name: 'Thời gian ngâm hạt',
  159. value:
  160. '${cropPlot.soakSeedsTime ?? '--'}'),
  161. WidgetRowPlotInfo(
  162. color: AppColors.DEFAULT
  163. .withOpacity(0.1),
  164. name: 'Ngày vô khây ươm',
  165. value:
  166. '${cropPlot.seedIncubationTime ?? '--'}'),
  167. WidgetRowPlotInfo(
  168. color: AppColors.DEFAULT
  169. .withOpacity(0.3),
  170. name: 'Số lượng cây trồng',
  171. value:
  172. '${cropPlot.numberPlants ?? '--'}'),
  173. WidgetRowPlotInfo(
  174. color: AppColors.DEFAULT
  175. .withOpacity(0.1),
  176. name: 'Số lượng cây hiện tại',
  177. value:
  178. '${cropPlot.numberCurrentPlants ?? '--'}'),
  179. WidgetRowPlotInfo(
  180. color: AppColors.DEFAULT
  181. .withOpacity(0.3),
  182. name: 'Ngày kết thúc canh tác',
  183. value:
  184. '${cropPlot.tbCropDTO.endDate ?? '--'}'),
  185. WidgetRowPlotInfo(
  186. color: AppColors.DEFAULT
  187. .withOpacity(0.1),
  188. name: 'Kỹ sư trực tiếp',
  189. value: '$technicians'),
  190. WidgetRowPlotInfo(
  191. color: AppColors.DEFAULT
  192. .withOpacity(0.3),
  193. name: 'Diện tích (m\u00B2)',
  194. value:
  195. '${cropPlot.tbCropDTO.areaM2.formatNumtoStringDecimal()}'),
  196. SizedBox(
  197. height: 8,
  198. ),
  199. _descriptionField(),
  200. SizedBox(
  201. height: 16,
  202. ),
  203. GetBuilder<
  204. DescriptionChangeControler>(
  205. builder: (_) {
  206. return SizedBox(
  207. width: double.infinity,
  208. height: 55,
  209. child: FlatButton(
  210. onPressed:
  211. controller.isChanged ==
  212. false
  213. ? () {}
  214. : () {
  215. FocusScopeNode
  216. currentFocus =
  217. FocusScope.of(
  218. context);
  219. if (!currentFocus
  220. .hasPrimaryFocus) {
  221. currentFocus
  222. .unfocus();
  223. }
  224. _validateInputs();
  225. },
  226. color: controller.isChanged
  227. ? AppColors.DEFAULT
  228. : Colors.grey[400],
  229. shape:
  230. RoundedRectangleBorder(
  231. borderRadius:
  232. new BorderRadius
  233. .circular(7.0),
  234. ),
  235. child: Text(
  236. 'Cập nhật'
  237. .toUpperCase(),
  238. style: TextStyle(
  239. fontWeight:
  240. FontWeight.bold,
  241. color:
  242. AppColors.WHITE,
  243. fontSize: 18)),
  244. ),
  245. );
  246. },
  247. )
  248. ],
  249. ),
  250. ));
  251. } else if (snapshot.hasError) {
  252. return Center(
  253. child: Text(snapshot.error.toString()),
  254. );
  255. } else {
  256. return LoadingListPage();
  257. }
  258. }))))));
  259. @override
  260. void dispose() {
  261. _descriptionController.dispose();
  262. super.dispose();
  263. }
  264. @override
  265. bool get wantKeepAlive => true;
  266. }
  267. class DescriptionChangeControler extends GetxController {
  268. bool isChanged = false;
  269. void initValue() {
  270. isChanged = false;
  271. update();
  272. }
  273. void changeValue(String oldValue, String newValue) {
  274. if (oldValue != newValue) {
  275. isChanged = true;
  276. } else {
  277. isChanged = false;
  278. }
  279. if (oldValue.isNullOrBlank && newValue.isEmpty) {
  280. isChanged = false;
  281. }
  282. update();
  283. }
  284. }