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.

393 lines
16KB

  1. import 'dart:convert';
  2. import 'package:farm_tpf/custom_model/Plant.dart';
  3. import 'package:farm_tpf/custom_model/SuppliesUsing.dart';
  4. import 'package:farm_tpf/data/api/app_exception.dart';
  5. import 'package:farm_tpf/data/repository/repository.dart';
  6. import 'package:farm_tpf/presentation/custom_widgets/bloc/media_helper_bloc.dart';
  7. import 'package:farm_tpf/presentation/custom_widgets/widget_loading.dart';
  8. import 'package:farm_tpf/presentation/custom_widgets/widget_media_picker.dart';
  9. import 'package:farm_tpf/presentation/screens/actions/bloc/action_detail_bloc.dart';
  10. import 'package:farm_tpf/presentation/screens/actions/plant/widget_plant_supply.dart';
  11. import 'package:farm_tpf/presentation/screens/actions/state_management_helper/change_file_controller.dart';
  12. import 'package:farm_tpf/utils/const_common.dart';
  13. import 'package:farm_tpf/utils/const_string.dart';
  14. import 'package:farm_tpf/utils/const_style.dart';
  15. import 'package:farm_tpf/utils/pref.dart';
  16. import 'package:farm_tpf/utils/validators.dart';
  17. import 'package:flutter/material.dart';
  18. import 'package:flutter_bloc/flutter_bloc.dart';
  19. import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
  20. import 'package:get/get.dart';
  21. import 'package:intl/intl.dart';
  22. import 'package:keyboard_dismisser/keyboard_dismisser.dart';
  23. import 'package:pattern_formatter/pattern_formatter.dart';
  24. import 'package:farm_tpf/utils/formatter.dart';
  25. import '../util_action.dart';
  26. class EditActionPlantScreen extends StatefulWidget {
  27. final int cropId;
  28. final bool isEdit;
  29. final int activityId;
  30. EditActionPlantScreen(
  31. {@required this.cropId, this.isEdit = false, this.activityId});
  32. @override
  33. _EditActionPlantScreenState createState() => _EditActionPlantScreenState();
  34. }
  35. class _EditActionPlantScreenState extends State<EditActionPlantScreen> {
  36. final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
  37. final _repository = Repository();
  38. GlobalKey<FormState> _formKey = GlobalKey();
  39. bool _autoValidate = false;
  40. Plant _plant = Plant();
  41. var pref = LocalPref();
  42. final _descriptionController = TextEditingController();
  43. final _quantityController = TextEditingController();
  44. final _densityController = TextEditingController();
  45. final _executeByController = TextEditingController();
  46. List<SuppliesUsing> suppliesUsing = new List<SuppliesUsing>();
  47. String executeTimeView;
  48. DateTime executeTime = DateTime.now();
  49. List<String> filePaths = List<String>();
  50. var changeFileController = Get.put(ChangeFileController());
  51. Future<Null> getSharedPrefs() async {
  52. var currentFullName = await pref.getString(DATA_CONST.CURRENT_FULL_NAME);
  53. _executeByController.text = currentFullName ?? "";
  54. }
  55. @override
  56. void initState() {
  57. super.initState();
  58. getSharedPrefs();
  59. changeFileController.initValue();
  60. _plant.suppliesUsing = new List<SuppliesUsing>();
  61. var parsedExecuteDate =
  62. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(executeTime);
  63. _plant.executeDate = "$parsedExecuteDate";
  64. executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(executeTime);
  65. _plant.cropId = widget.cropId;
  66. }
  67. _validateInputs() async {
  68. if (_formKey.currentState.validate()) {
  69. _formKey.currentState.save();
  70. LoadingDialog.showLoadingDialog(context);
  71. filePaths = Get.find<ChangeFileController>().files;
  72. List<SuppliesUsing> newSups = [];
  73. suppliesUsing.forEach((sup) {
  74. var newSup = sup;
  75. newSup.suppliesInWarehouseId = sup.tbSuppliesInWarehouseId;
  76. newSups.add(newSup);
  77. });
  78. _plant.suppliesUsing = newSups;
  79. var activityPlant = jsonEncode(_plant.toJson()).toString();
  80. //ADD NEW
  81. if (_plant.activityId == null) {
  82. _repository.createAction((value) {
  83. LoadingDialog.hideLoadingDialog(context);
  84. Get.back(result: value);
  85. Get.snackbar(label_add_success, "Hoạt động trồng",
  86. snackPosition: SnackPosition.BOTTOM);
  87. }, (error) {
  88. LoadingDialog.hideLoadingDialog(context);
  89. _scaffoldKey.currentState.showSnackBar(SnackBar(
  90. content: Row(
  91. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  92. children: <Widget>[
  93. Flexible(child: Text(AppException.handleError(error))),
  94. Icon(Icons.error),
  95. ],
  96. ),
  97. backgroundColor: Colors.red,
  98. duration: Duration(seconds: 3),
  99. ));
  100. },
  101. apiAddAction: ConstCommon.apiAddPlant,
  102. paramActivity: ConstCommon.paramsActionPlant,
  103. activityAction: activityPlant,
  104. filePaths: filePaths);
  105. } else {
  106. //UPDATE
  107. _repository.updateAction((value) {
  108. LoadingDialog.hideLoadingDialog(context);
  109. Get.back(result: value);
  110. Get.snackbar(label_update_success, "Hoạt động trồng",
  111. snackPosition: SnackPosition.BOTTOM);
  112. }, (error) {
  113. LoadingDialog.hideLoadingDialog(context);
  114. _scaffoldKey.currentState.showSnackBar(SnackBar(
  115. content: Row(
  116. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  117. children: <Widget>[
  118. Flexible(child: Text(AppException.handleError(error))),
  119. Icon(Icons.error),
  120. ],
  121. ),
  122. backgroundColor: Colors.red,
  123. duration: Duration(seconds: 3),
  124. ));
  125. },
  126. apiUpdateAction: ConstCommon.apiUpdatePlant,
  127. paramActivity: ConstCommon.paramsActionPlant,
  128. activityAction: activityPlant,
  129. filePaths: filePaths);
  130. }
  131. } else {
  132. _autoValidate = true;
  133. }
  134. }
  135. Widget _btnExecuteTimePicker() {
  136. return FlatButton(
  137. padding: EdgeInsets.only(top: 0.0, right: 0.0, bottom: 0.0, left: 0.0),
  138. onPressed: () {
  139. DatePicker.showDateTimePicker(context,
  140. showTitleActions: true, onChanged: (date) {}, onConfirm: (date) {
  141. setState(() {
  142. var parsedDate =
  143. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(date);
  144. _plant.executeDate = "$parsedDate";
  145. executeTimeView = DateFormat("dd/MM/yyyy HH:mm").format(date);
  146. });
  147. }, currentTime: executeTime, locale: LocaleType.vi);
  148. },
  149. child: Container(
  150. padding:
  151. EdgeInsets.only(top: 0.0, right: 0.0, bottom: 10.5, left: 0.0),
  152. decoration: BoxDecoration(
  153. border: kBorderTextField,
  154. ),
  155. child: Row(
  156. children: [
  157. Expanded(
  158. child: Text(
  159. //TODO: check condition
  160. executeTimeView == null ? "$executeTime" : executeTimeView,
  161. style: TextStyle(fontSize: 14.0, color: Colors.black87),
  162. )),
  163. Icon(
  164. Icons.date_range,
  165. color: Colors.blue,
  166. ),
  167. ],
  168. )));
  169. }
  170. Widget _quantityField() {
  171. return TextFormField(
  172. keyboardType: TextInputType.numberWithOptions(decimal: true),
  173. inputFormatters: [
  174. ThousandsFormatter(
  175. formatter: NumberFormat("#,###.##", "es"), allowFraction: true)
  176. ],
  177. decoration: InputDecoration(labelText: "Số lượng cây trồng"),
  178. controller: _quantityController,
  179. onSaved: (newValue) {
  180. _plant.quantity = newValue.parseDoubleThousand();
  181. },
  182. );
  183. }
  184. Widget _densityField() {
  185. return TextFormField(
  186. keyboardType: TextInputType.text,
  187. decoration: InputDecoration(labelText: "Mật độ"),
  188. controller: _densityController,
  189. onSaved: (newValue) {
  190. _plant.density = newValue;
  191. },
  192. );
  193. }
  194. Widget _desciptionField() {
  195. return TextFormField(
  196. keyboardType: TextInputType.text,
  197. decoration: InputDecoration(labelText: "Ghi chú"),
  198. controller: _descriptionController,
  199. onSaved: (newValue) {
  200. _plant.description = newValue;
  201. },
  202. );
  203. }
  204. Widget _executeByField() {
  205. return TextFormField(
  206. keyboardType: TextInputType.text,
  207. decoration: InputDecoration(labelText: "Người thực hiện"),
  208. enabled: false,
  209. controller: _executeByController,
  210. onSaved: (newValue) {},
  211. );
  212. }
  213. _actionAppBar() {
  214. IconButton iconButton;
  215. if (1 == 1) {
  216. iconButton = IconButton(
  217. icon: Icon(
  218. Icons.done,
  219. color: Colors.black,
  220. ),
  221. onPressed: () {
  222. FocusScopeNode currentFocus = FocusScope.of(context);
  223. if (!currentFocus.hasPrimaryFocus) {
  224. currentFocus.unfocus();
  225. }
  226. _validateInputs();
  227. },
  228. );
  229. return <Widget>[iconButton];
  230. }
  231. return <Widget>[Container()];
  232. }
  233. @override
  234. Widget build(BuildContext context) => KeyboardDismisser(
  235. gestures: [
  236. GestureType.onTap,
  237. GestureType.onPanUpdateDownDirection,
  238. ],
  239. child: Scaffold(
  240. key: _scaffoldKey,
  241. appBar: AppBar(
  242. centerTitle: true,
  243. title: Text(plot_action_plant),
  244. actions: _actionAppBar()),
  245. body: KeyboardDismisser(
  246. child: MultiBlocProvider(
  247. providers: [
  248. BlocProvider<ActionDetailBloc>(
  249. create: (context) =>
  250. ActionDetailBloc(repository: Repository())
  251. ..add(FetchData(
  252. isNeedFetchData: widget.isEdit,
  253. apiActivity: ConstCommon.apiDetailPlant,
  254. activityId: widget.activityId))),
  255. BlocProvider<MediaHelperBloc>(
  256. create: (context) =>
  257. MediaHelperBloc()..add(ChangeListMedia(items: [])),
  258. )
  259. ],
  260. child: Form(
  261. key: _formKey,
  262. autovalidate: _autoValidate,
  263. child: SingleChildScrollView(
  264. padding: EdgeInsets.all(8.0),
  265. child: BlocConsumer<ActionDetailBloc,
  266. ActionDetailState>(
  267. listener: (context, state) async {
  268. if (state is ActionDetailFailure) {
  269. LoadingDialog.hideLoadingDialog(context);
  270. } else if (state is ActionDetailSuccess) {
  271. LoadingDialog.hideLoadingDialog(context);
  272. _plant = Plant.fromJson(state.item);
  273. _plant.activityId = widget.activityId;
  274. _quantityController.text = _plant.quantity
  275. .formatNumtoStringDecimal();
  276. _densityController.text = _plant.density;
  277. _descriptionController.text =
  278. _plant.description;
  279. _executeByController.text = _plant.executeBy;
  280. try {
  281. executeTime =
  282. DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
  283. .parse(_plant.executeDate);
  284. } catch (_) {}
  285. executeTimeView =
  286. DateFormat("dd/MM/yyyy HH:mm")
  287. .format(executeTime);
  288. //Show media
  289. if (_plant.media != null) {
  290. await UtilAction.cacheFiles(_plant.media)
  291. .then((value) {
  292. BlocProvider.of<MediaHelperBloc>(context)
  293. .add(ChangeListMedia(items: value));
  294. }).whenComplete(() {
  295. print("completed");
  296. });
  297. }
  298. //list supply
  299. suppliesUsing = _plant.suppliesUsing;
  300. Get.find<ChangeSupplyUsing>()
  301. .changeInitList(suppliesUsing);
  302. } else if (state is ActionDetailInitial) {
  303. print("init");
  304. } else if (state is ActionDetailLoading) {
  305. print("loading");
  306. LoadingDialog.showLoadingDialog(context);
  307. }
  308. },
  309. builder: (context, state) {
  310. return Column(
  311. children: <Widget>[
  312. Container(
  313. width: double.infinity,
  314. child: Text(
  315. "Ngày thực hiện *",
  316. style: TextStyle(
  317. color: Colors.black54,
  318. fontSize: 13.0),
  319. ),
  320. ),
  321. _btnExecuteTimePicker(),
  322. SizedBox(
  323. height: 8.0,
  324. ),
  325. _quantityField(),
  326. SizedBox(
  327. height: 8.0,
  328. ),
  329. _densityField(),
  330. SizedBox(
  331. height: 8.0,
  332. ),
  333. _desciptionField(),
  334. SizedBox(
  335. height: 8.0,
  336. ),
  337. _executeByField(),
  338. SizedBox(
  339. height: 8.0,
  340. ),
  341. WidgetPlantSupply(
  342. currentItems: [],
  343. onChangeSupplies: (value) {
  344. suppliesUsing = value;
  345. }),
  346. SizedBox(
  347. height: 8.0,
  348. ),
  349. BlocBuilder<MediaHelperBloc,
  350. MediaHelperState>(
  351. builder: (context, state) {
  352. if (state is MediaHelperSuccess) {
  353. return WidgetMediaPicker(
  354. currentItems: state.items,
  355. onChangeFiles: (filePaths) async {
  356. Get.find<ChangeFileController>()
  357. .addAllFile(filePaths);
  358. });
  359. } else {
  360. return Center(
  361. child: CircularProgressIndicator());
  362. }
  363. }),
  364. ],
  365. );
  366. },
  367. ),
  368. ))))));
  369. @override
  370. void dispose() {
  371. _quantityController.dispose();
  372. _densityController.dispose();
  373. _descriptionController.dispose();
  374. _executeByController.dispose();
  375. super.dispose();
  376. }
  377. }